我有以下关联:
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
我想知道do_other_stuff中的销毁是否由dependent=>destroy触发,因为部分销毁将在do_some_stuff 中完成
我尝试了parent.destroyed?
、parent.marked_for_destruction?
、parent.frozen?
,但都不起作用:/
有什么想法吗?
您可以使用关联回调(before_remove
或after_remove
)
class Parent < ActiveRecord::Base
has_many :children, :dependent => :destroy, :before_remove => :do_foo
before_destroy :do_bar
def do_bar
end
def do_foo
end
end
也许是这样的:
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