gpt4 book ai didi

ruby-on-rails - 如何触发对象的销毁回调,该对象是自动删除该对象的连接模型的一部分?

转载 作者:行者123 更新时间:2023-12-04 05:55:02 24 4
gpt4 key购买 nike

rails 2.3.8。我有 3 个模型,用户、来源和订阅。

User  attr_accessible   :source_ids
has_many :subscriptions
has_many :sources, :through => :subscriptions

Source has_many :subscriptions

Subscription belongs_to :user
belongs_to :source

我有一个界面,允许用户编辑他们对源的订阅。它收集 source_ids,并根据该集合创建或删除订阅。我遇到的问题是,引用:

"Automatic deletion of join models is direct, no destroy callbacks are triggered."



订阅正在被删除,而不是被销毁。我在订阅模型中有一个未触发的回调:
before_destroy do |subscription|
[Some irrelevant object not to be mentioned].destroy
end

我的问题是,由于加入模型而自动删除订阅时,如何触发此回调?

最佳答案

回复您在HMT collection_singular_ids= deletion of join models is direct, no destroy callbacks are triggered的回复

改变这一行:

 has_many :users, :through => :memberships

对此:
 has_many :users, :through => :memberships, :after_remove => :your_custom_method

并在 User 模型中定义 protected your_custom_method。这样,当用户删除对某个源的订阅时,就会调用此方法。

祝你好运!

关于ruby-on-rails - 如何触发对象的销毁回调,该对象是自动删除该对象的连接模型的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4839895/

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