Forum: Ruby on Rails when deleeting a post, delete all comments too

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1bb9fa50b2cc637379905b9994cd16b8?d=identicon&s=25 heman (Guest)
on 2006-06-01 20:22
i can't quite figure out how to delete all of a posts comments along
with deleting the post. ive set up comments as belonging to posts and
posts as having many comments and...

Post.find(params[:id]).destroy

...works fine i just can't figure out any way to delete that posts
comments as well. comments uses the standard foreign key of post_id.

thanks.
3036c74b9c278dc525af5692080a6d80?d=identicon&s=25 Jeff Ward (Guest)
on 2006-06-01 20:27
(Received via mailing list)
In your post.rb file, add :dependent => :destroy....

eg:
has_many :comments, :dependent => :destroy
1bb9fa50b2cc637379905b9994cd16b8?d=identicon&s=25 heman (Guest)
on 2006-06-01 20:30
perfect, thank you!
This topic is locked and can not be replied to.