destroy_all with conditions

In a has_many relationship, there should be a way to add conditions to destroy in order to eliminate an explicit find.

[sourcecode language="ruby"]
@post.comments.find(:all, :conditions => “name LIKE ’stupid%’”).each {|record| record.destroy }
[/sourcecode]

With a patch I recently submitted to Rails core, you can now trim this line into:

[sourcecode language="ruby"]
@post.comments.destroy_all “name LIKE ’stupid%’”
[/sourcecode]

Jump on over to Lighthouse bugtracker and try out the patch.


Leave a Reply