Robert Pankowecki
2010-Dec-28 16:27 UTC
has_many :dependent => :destroy and association callbacks
Today I noticed that calling object.collection.destroy_all leads to calling association callbacks like ex. :before_remove but object.destroy does not call those callbacks when configured with :dependent => :destroy option. You can find that hbtm uses collection.clear which under the hoods calls collection.destroy_all however has_many uses the metod configure_dependency_for_has_many to setup the hooks ( https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb#L1000 ) which iterates over the collection and sends destroy() to every object (https://github.com/rails/rails/blob/master/activerecord/lib/ active_record/associations.rb#L1625) I think it could be a good thing to make has_many behave more like hbtm and just use collection.destroy_all . This way also the association callbacks would get called. Also probably the optimization fix with counter_method could probably be move to the call in destroy_all. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Jon Leighton
2010-Dec-30 11:42 UTC
Re: has_many :dependent => :destroy and association callbacks
Hiya, I am working on refactoring this section of the code at the moment actually, and this problem will be fixed when I''m done. Cheers On Tue, 2010-12-28 at 08:27 -0800, Robert Pankowecki wrote:> Today I noticed that calling object.collection.destroy_all leads to > calling association callbacks like ex. :before_remove but > object.destroy does not call those callbacks when configured > with :dependent => :destroy option. > > You can find that hbtm uses collection.clear which under the hoods > calls collection.destroy_all however has_many uses the metod > configure_dependency_for_has_many to setup the hooks ( > https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb#L1000 > ) > which iterates over the collection and sends destroy() to every object > (https://github.com/rails/rails/blob/master/activerecord/lib/ > active_record/associations.rb#L1625) > > I think it could be a good thing to make has_many behave more like > hbtm and just use collection.destroy_all . This way also the > association callbacks would get called. Also probably the optimization > fix with counter_method could probably be move to the call in > destroy_all. >-- http://jonathanleighton.com/
Robert Pankowecki
2010-Dec-30 11:56 UTC
Re: has_many :dependent => :destroy and association callbacks
I''m glad to hear it. Robert Pankowecki -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.