rails/rails — Issue #5205: has_many :dependent is executed before before_destroy callbacks
Author: mbillard
Created: 2012-02-28 16:33:57+00:00
State: open

The option set on the has_many :dependent option is evaluated before any before_destroy callbacks.

Example:

class Item < ActiveRecord::Base
belongs_to :order
end

class Order < ActiveRecord::Base
has_many :items, :dependent => :nullify
before_destroy :cancel_items

def cancel_items
# self.items is empty because all the items have had their order_id nullified (or destroyed/deleted if :dependent is set to these)
end
end

There's a workaround that necessitates to put the before_destroy definition above the has_many, but this isn't obvious and still counter-intuitive.