gpt4 book ai didi

ruby-on-rails - 如何知道模型何时被 :dependent => :destroy in rails? 自动销毁

转载 作者:行者123 更新时间:2023-12-04 20:51:50 24 4
gpt4 key购买 nike

我有以下关联:

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 中知道销毁是否已被依赖 => 销毁,因为它的一部分将/将在 do_some_stuff 中完成

我试过 parent.destroyed? , parent.marked_for_destruction? , parent.frozen?但没有任何效果:/

有任何想法吗?

最佳答案

您可以使用关联回调( before_removeafter_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

关于ruby-on-rails - 如何知道模型何时被 :dependent => :destroy in rails? 自动销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9788024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com