I have
class MyContainer < ActiveRecord::Base
:has_many MyObjects, :dependent => :destroy
end
I want to delete all the MyObjects in the container without having to delete the MyContainer. My model does have :dependent => :destroy, however I don’t want to have to delete and re-create the object because it is slower.
Something like this does not work:
@obj = MyContainer.find_by_id(10)
@obj.my_objects.delete_all
How can I accomplish this?
delete_allis anActiveRecord::Baseclass method.You should use
destroy_all. Something like:Using
delete_allproperly would be faster if you don’t need to lookup yourMyContainerfirst (or use it for other stuff)EDIT: for rails 3