Полагаю, у вас есть такая модель:
class Account < ActiveRecord::Base
has_and_belongs_to_many :users
end
Чтобы переопределить Account#users<<
, вам нужно определить его в блоке, который вы передаете has_and_belongs_to_many
:
class Account < ActiveRecord::Base
has_and_belongs_to_many :users do
def <<(user)
# ...
end
end
end
Вы можете получить доступ к соответствующему Account
объекту, обратившись к proxy_association.owner
:
def <<(user)
account = proxy_association.owner
end
Чтобы позвонить оригиналу Account#users<<
, позвоните Account#users.concat
:
def <<(user)
account = proxy_association.owner
# user = do_something(user)
account.users.concat(user)
end
Подробнее см. На этой странице: Расширения ассоциации - ActiveRecord