ActiveRecord 関連付けの保存に関して問題があり、あなたの助けが必要です :)
記事をマージする機能をレガシー コードに追加する必要があります。
次のように動作すると予想されます。
- 「ソース」記事のテキストを「ターゲット」記事にマージします。
- 「ソース」のコメントを確認し、もしあれば、それらを「ターゲット」に再度関連付けます。
- 「ソース」記事を破棄します。コメントは保存し、「ターゲット」に関連付ける必要があります。
これが私の Article モデルコードです (読みやすくするために縮小されています)。
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