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 Zaidi wrote:

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?

Look into adding :after_add callbacks to the associations:

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