如何知道模型何时被自动去除:依赖=>:在rails中销毁?

Syl*_*aux 5 activerecord ruby-on-rails associations destroy

我有以下关联:

class Parent < ActiveRecord::Base
  has_many :children, :dependent => :destroy
  before_destroy :do_some_stuff
end

class Child < ActiveRecord::Base
  belongs_to :parent
  before_destroy :do_other_stuff
end
Run Code Online (Sandbox Code Playgroud)

我想在do_other_stuff中知道,如果销毁是由dependent => destroy发生的,因为它的一部分会/将在do_some_stuff中完成

我试过parent.destroyed?,parent.marked_for_destruction?,parent.frozen?但没有工作:/

有任何想法吗?

Grz*_*zek 0

也许是这样的:

class Parent < ActiveRecord::Base
    has_many :children
    before_destroy :some_stuff
    def some_stuff
        children.each do |child|
            child.parent_say_bye
        end
    end
end

class Child < ActiveRecord::Base
    belongs_to :parent
    before_destroy :do_other_stuff
    def parent_say_bye
        #do some stuff
        delete
    end
end
Run Code Online (Sandbox Code Playgroud)