views:

19

answers:

2

I have an Article which has_many Comments.

When i create comments i can use "new" to build them in memory, and the comment records only get created when the article is saved.

Does such a mechanism exist for marking comments for deletion, so that their records are only removed when the article is saved?

thanks.

+1  A: 

do it in a transaction:

Article.transaction do
   ...
end
KARASZI István
+3  A: 

I suggest you get familiar with #accepts_nested_attributes_for. The example there is essentially what you wanted. This is a rewritten one:

class Post < ActiveRecord::Base
  has_many :comments
  accepts_nested_attributes_for :comments, :allow_destroy => true
end

post = Post.find(1) # With 3 comments
post.comments_attributes = [{:_destroy => "1", :id => post.comments.first.id}]
# Look ma! No SQL statements!
post.save!
# BEGIN / UPDATE posts / DELETE FROM comments WHERE id = X / COMMIT
François Beausoleil