Howto use callbacks in a has_many through association?

rails callbacks
has_many :through rails
has_one :through
rails inverse_of
rails polymorphic
belongs to many rails
after_add rails
rails references

I have a Task model associated to a Project model via has_many through and need to manipulate the data before delete/insert via the association.

Since "Automatic deletion of join models is direct, no destroy callbacks are triggered." i can not use callbacks for this.

In Task i need all project_ids to calculate a value for Project after Task is saved. How can i disable delete or change delete to destroy on has_many through association? What is best practise for this problem?

class Task
  has_many :project_tasks
  has_many :projects, :through => :project_tasks

class ProjectTask
  belongs_to :project
  belongs_to :task

class Project
  has_many :project_tasks
  has_many :tasks, :through => :project_tasks

Seems like i have to use associations callbacks before_add, after_add, before_remove or after_remove

class Task
  has_many :project_tasks
  has_many :projects, :through => :project_tasks, 
                      :before_remove => :my_before_remove, 
                      :after_remove => :my_after_remove
  protected

  def my_before_remove(obj)
    ...
  end

  def my_after_remove(obj)
    ...
  end
end   

Problem with callbacks and has_many :through association, call a function that updates a score of the added venue. And here's the problem: I can't use after_add/after_remove callbacks in Reply model,� The :source option specifies the source association name for a has_many :through association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name. 4.3.2.10 :source_type

This is what I did

in the model:

class Body < ActiveRecord::Base
  has_many :hands, dependent: destroy
  has_many :fingers, through: :hands, after_remove: :touch_self
end

in my Lib folder:

module ActiveRecord
  class Base
  private
    def touch_self(obj)
      obj.touch && self.touch
    end
  end
end

Active Record Associations — Ruby on Rails Guides, I was reminded of this fact recently while pairing with a … we eventually decided to go another route and use a different callback function. on a has_many or has_many, through: collection association, our callbacks can be� How a has_many :through association works in practice. When you’re beginning to work with Rails and ActiveRecord, it can be tough to wrap your head around the concept of has_many :through associations, and more importantly how they can be used in your Rails app.

Updating joins model associations, Rails add and remove records on the collection. To remove the records, Rails use the delete method and this one will not call any destroy callback.

You can force Rails to call destroy instead delete when is removing records. To do that, install the gem replace_with_destroy and pass the option replace_with_destroy: true to the has_many association.

class Task
  has_many :project_tasks
  has_many :projects, :through => :project_tasks,
            replace_with_destroy: true
  ...
end

class ProjectTask
  belongs_to :project
  belongs_to :task

  # any destroy callback in this model will be executed
  #...

end

class Project
  ...
end

With this, you ensure Rails invoke all the destroy callbacks. This could be very useful if you are using paranoia.

Functions to Call Upon: ActiveRecord Association Callbacks, With a has_many association defined with a through option, the callbacks The output should include both a valid consultant_id and purpose_id , for example: Fix callbacks on has_many :through associations #35799. has_many :through. A has_many :through association is used to setup a many to many relationship with another model in your application. This association uses a join table to connect the models allowing each to both ‘has’ and ‘belong_to’ each model. In this case, our cop can have many perps to bust and our perps can belong to the many cops.

It seems like adding dependent: :destroy to has_many :through relation will destroy the join model (instead of delete). This is because CollectionAssociation#delete internally refers to :dependent option to determine whether the passed records should be deleted or destroyed.

So in your case,

class Task
  has_many :project_tasks
  has_many :projects, :through => :project_tasks, :dependent => :destroy
end

should work.

has_many-through-join-model record is incomplete in `after_add , This will also run validations and callbacks of associated object(s). If using with the :through option, the association on the join model must be a #belongs_to,� AKA "has many through" Overview. Many-to-many through associations behave in the same way as many-to-many associations, except that in a many-to-many through association, the join table is created automatically. In a many-to-many through assocation, you define a model containing two fields that correspond to the two models you will be joining

has_many (ActiveRecord::Associations::ClassMethods), So a straightway solution is setup all associations with dependent: :destroy in Client objects to be deleted directly from the database (so callbacks will not execute) To demonstrate, let me set up an example model: An important caveat with going through has_one or has_many associations on the join� I have a class with a has_many association. I have a after_add and an after_remove callback on the association, like this: # rails 4.2.0.alpha has_many :character_levels, after_add: :log_adding_event, after_remove: :log_removing_event de

Ruby on Rails: how to efficiently delete a mass of associated objects , If the :through option is true callbacks in the join models are triggered except So a Person class that makes a #has_many association will use “person_id” as� The has_many :through association is also useful for setting up “shortcuts” through nested has_many associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document.

Method: ActiveRecord::Associations::ClassMethods#has_many , The Rails 4 documentation says this regarding destroy callbacks on the join model for a has_many :through relationship: collection=objects� The has_many :through association is also useful for setting up "shortcuts" through nested has_many associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document.