I assume you have a model like this:
class Account < ActiveRecord::Base
has_and_belongs_to_many :users
end
To override Account#users<<
, you need to define it in a block that you pass to has_and_belongs_to_many
:
class Account < ActiveRecord::Base
has_and_belongs_to_many :users do
def <<(user)
# ...
end
end
end
You can access the appropriate Account
object by referring to proxy_association.owner
:
def <<(user)
account = proxy_association.owner
end
To call the original Account#users<<
, call Account#users.concat
:
def <<(user)
account = proxy_association.owner
# user = do_something(user)
account.users.concat(user)
end
For more details, see this page: Association extensions - ActiveRecord