How to trigger trigger callbacks for an object that is part of a union model that automatically deletes this object?

Rails 2.3.8. I have 3 models: user, source and subscription.

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

Source       has_many   :subscriptions

Subscription belongs_to :user
             belongs_to :source

I have an interface that allows the user to edit their Subscriptions to the Source. It collects source_ids and creates or deletes a subscription based on the collection. The problem I ran into is the quote:

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

Subscriptions are deleted, not destroyed. I have a callback in a Subscription model that does not start:

before_destroy do |subscription|
  [Some irrelevant object not to be mentioned].destroy
end

My question is, how can I call this callback when the subscription is automatically deleted due to the connection model?

+5
2

HMT collection_singular_ids = ,

:

 has_many :users, :through => :memberships

:

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

protected your_custom_method . , - , .

!

+5
@user.subscriptions.delete
has_many   :subscriptions, :dependent => :destroy    # <- setting this on the association  will destroy the related subscriptions
has_many   :subscriptions, :dependent => :delete_all # <- setting this on the association  will delete the related subscriptions

rdoc:

collection.delete(,...)
, NULL. , :dependent => :destroy , :dependent => :delete_all

+2

All Articles