This project is archived and is in readonly mode.

#1079 ✓resolved
Ben VandenBos

belongs_to :dependent => :destroy should destroy self before assocation

Reported by Ben VandenBos | September 19th, 2008 @ 11:36 PM | in 2.x

:belongs_to :dependent => :destroy leaves the model open to foreign key constraint failure upon deletion. This seems be due to it using before_destroy as opposed to after_destroy. It can't destroy the associated model before it deletes itself because of the foreign key. I believe the fix is to simply use after_destroy (patch attached) to destroy :belongs_to associations after it's destroyed itself.

Example:


class Person < ActiveRecord::Base
  belongs_to :person_address, :dependent => :destroy
end

class PersonAddress < ActiveRecord::Base
  has_one :person
end

...and a foreign key on person_address_id on the person table to id on the person_address table.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Referenced by

Pages