Callback when objects connect as a habtm relationship

If Product and Category models are in a habtm relationship, i.e.

class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
end

class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end

I want a piece of code to be executed every time a product is connected
to a
category.
Where do I put this code? Which callback (and on which model) will be
triggered?

Any hints appreciated. Thanks for help.

Kazim Z. wrote:

I want a piece of code to be executed every time a product is connected
to a category.
Where do I put this code? Which callback (and on which model) will be
triggered?

Look into adding :after_add callbacks to the associations:

http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M001602


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs