This project is archived and is in readonly mode.
destroy_all with conditions on has_many association
-
David Stevenson
+1... associations should have a destroy_all method if they have a delete_all method.
-
DHH
- State changed from new to duplicate
This is already possible with the destroy_all on AssociationCollection. Your example would be @post.comments.all(:conditions => "name LIKE 'stupid%'").destroy_all.
-
Andrew White
Just want to add to this two comments as my first instinct when needing to restrict a destroy_all was to pass conditions in.
Firstly the example provided by DHH is incorrect - you need to use
scopedrather thanall. Secondly, while using scoped works I'd like to see consistency between the collection methods and AR::Base methods - so delete and destroy should accept ids and delete_all and destroy_all should accept conditions.
