有關於節能ActiveRecord關聯的問題,需要你的幫助:)Rails的:ActiveRecord關聯的節約問題
我需要添加文章合併功能遺留代碼。
它的預期的工作方式如下:
- 合併「源」文章的文本「目標」的文章。
- 檢查「源」的評論,如果有的話,將它們重新關聯到「目標」。
- 銷燬「來源」文章。評論應該保留並與「目標」相關聯。
這裏是我的文章模型代碼(降低可讀性)。
class Article < Content
before_destroy :reload_associated_comments
has_many :comments, :dependent => :destroy, :order => "created_at ASC" do
def reload_associated_comments
unless self.comments.empty?
article = Article.find(@merge_with)
self.comments.each do |comment|
comment.article = article
article.save!
end
end
end
def merge_with(id)
@merge_with = id
article = Article.find(@merge_with)
if !article.nil?
text = article.body + body
article.body = text
article.save!
self.destroy
return article
end
nil
end
end
下面的評論模型(也降低):
class Comment < Feedback
belongs_to :article
end
的問題是,當我從before_destroy掛鉤什麼也沒有保存到數據庫返回。我通過以下檢查:
eval Article.find(target_article_id).comments
保存引發沒有例外。我在這裏錯過了什麼?
在此先感謝!
這爲我工作
def merge_with(id)
@merge_with = id
article = Article.find(@merge_with)
unless article.nil?
text = article.body + body
article.body = text
article.save!
reload_associated_comments
self.reload
self.destroy
return article
end
nil
end
在該方法reload_associated_comments,它應該是comment.save!而不是article.save!我認爲:) – Zippie 2013-03-13 22:50:13
也我不知道,但我認爲你不能調用self.destroy裏面的自我調用的方法。返回文章,然後從外部銷燬它(您稱之爲'merge_with'方法) – Zippie 2013-03-13 22:51:50
'reload_associated_comments'方法用作'before_destroy'回調。如果我打電話給article.save !,這是不是意味着自動保存了評論?如果沒有,保存的正確順序是什麼。評論和文章,反之亦然? – niebelung 2013-03-13 22:55:22