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.

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

I'm glad to hear it.

Robert Pankowecki