Have a problem regarding saving ActiveRecord associations and need your help :)
I need to add articles merging functionality to legacy code.
It's expected to work the following way:
- Merge "source" article's text to the "target" article.
- Check for "source"'s comments and, if any, re-associate them to the "target".
- Destroy the "source" article. Comments should be preserved and associated with "target".
Here's my Article model code (reduced for readability).
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
Here's Comment model (also reduced):
class Comment < Feedback
belongs_to :article
end
The problem is when I return from before_destroy hook nothing has been saved to the database. I check it via the following:
eval Article.find(target_article_id).comments
Save raises no exceptions. What I'm missing here?
Thanks in advance!
This worked for me
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