Skip to content

dependent => :destroy deletes children before "before_destroy" is executed #670

@lighthouse-import

Description

@lighthouse-import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/4386
Created by Jens - 2011-02-22 08:32:33 UTC

Problem: Upon destroying an ActiveRecord::Base object, the "before_destroy" method - which should trigger a transaction rollback if returning false - is only exceuted AFTER all child objects have been destroyed via ":dependent => :destroy".

However, this prevents the before_destroy method from seeing those same child objects, in case it needs them to determine whether the destruction should be successful.

Expected behaviour:
before_destroy should be called before any objects are destroyed, even child records. The before_destroy context should see the original state of the application as if "destroy" was never called. It should be executed within the "destroy" transaction, however, so that any changes it makes can be rolled back.

class Foo < AR::Base
 has_many :children, :dependent => :destroy
 has_many :grandchildren, :through => :children

 before_destroy :check
 def check
  # will always be true since all grandchildren have already been destroyed at this stage
  return self.grandchildren.still_there.empty?
 end
end

class Child < AR::Base
 has_many :grandchildren
 belongs_to :foo
end

class Grandchild < AR::Base
 belongs_to :child
 named_scope :still_there, :conditions => ...
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions