Forum: Ruby on Rails Callback when objects connect as a habtm relationship

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dfb5975ecda818fc5a0848a73e4736ae?d=identicon&s=25 Kazim Zaidi (Guest)
on 2008-12-16 16:46
(Received via mailing list)
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
Blog: http://tuxplayground.blogspot.com
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2008-12-18 03:00
(Received via mailing list)
Kazim Zaidi 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/As...


--
Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com
This topic is locked and can not be replied to.