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

Eri*_*ric 7 ruby-on-rails callback associations destroy

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
Run Code Online (Sandbox Code Playgroud)

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

"自动删除连接模型是直接的,不会触发销毁回调."

订阅正在删除,而不是销毁.我在订阅模型中有一个未触发的回调:

before_destroy do |subscription|
  [Some irrelevant object not to be mentioned].destroy
end
Run Code Online (Sandbox Code Playgroud)

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

Swa*_*rtz 6

HMT中回复您的回复collection_singular_ids =删除连接模型是直接的,不会触发销毁回调

改变这一行:

 has_many :users, :through => :memberships
Run Code Online (Sandbox Code Playgroud)

对此:

 has_many :users, :through => :memberships, :after_remove => :your_custom_method
Run Code Online (Sandbox Code Playgroud)

并在User模型中定义受保护的your_custom_method.这样,当用户将订阅删除到某个Source时,将调用此方法.

祝好运!