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.


@post.comments.find(:all, :conditions => "name LIKE 'stupid%'").each {|record| record.destroy }

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


@post.comments.destroy_all "name LIKE 'stupid%'"

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


Leave a Reply