Is there a way to add callbacks for when an item is added to a habtm relationship?
For example, I have the following two models, User
and Role
:
# user.rb
class User; has_and_belongs_to_many :roles; end
# role.rb
class Role; has_and_belongs_to_many :users; end
I want to add a callback to the <<
method (@user << @role
), but I can't seem to find an ActiveRecord callback because there is no model for the join table (because its a true habtm).
I'm aware that I could write a method like add_to_role(role)
, and define everything in there, but I'd prefer to use a callback. Is this possible?