rails/rails — Issue #4034: Rails 3.0: Destroy callbacks are not run when setting nested attributes on collection with :through association 
Author: dtognazzini
Created: 2011-12-19 20:21:02+00:00
State: open

This behavior was introduced under this commit:
https://github.com/rails/rails/commit/cef76c8af4705dc60f85a721e3a14adb99418d33

The issue has been fixed in Rails 3.1 and did not exist in Rails 2.3. See below for steps to reproduce.

Given

```ruby
class Contact < ActiveRecord::Base
has_many :contact_addresses, :dependent => :destroy
has_many :addresses, :through => :contact_addresses, :dependent => :destroy

accepts_nested_attributes_for :addresses, :allow_destroy => true
end

class Address < ActiveRecord::Base
has_many :contact_addresses, :dependent => :destroy
has_many :contacts, :through => :contact_addresses
end

class ContactAddress < ActiveRecord::Base
belongs_to :address
belongs_to :contact

acts_as_list :scope => :contact
end
```

This fails

```ruby
class ContactTest < ActiveSupport::TestCase
def test_address_attributes
address1 = Address.create!
address2 = Address.create!

contact = Contact.create!
contact.contact_addresses.build(:address => address1)
contact.contact_addresses.build(:address => address2)
contact.save!

contact.attributes = {
"addresses_attributes" => {
"0" => { "id" => address1.id.to_s, "_destroy" => "1" },
"1" => { "id" => address2.id.to_s },
}
}
contact.save!

assert_equal 1, contact.contact_addresses.first.position
end
end
```

This is because HasManyThroughAssociation.destroy() calls delete_records(). Are there any plans to fix this in Rails 3.0 ?