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.


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

class PersonAddress < ActiveRecord::Base
  has_one :person

...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=""></a>

People watching this ticket


Referenced by