How to override association proxy push()

i’m trying to override the push method on a habtm association, so i
can run code each time an element is added to the collection. the
module.included method is never being called when habtm includes the
module… how can i do this? or is there a better way to make a
‘before_save’ listener for just the association?

class User < ActiveRecord::Base
has_and_belongs_to_many :blacklisted_users,
:class_name => ‘User’,
:join_table => :blacklisted_users,
:foreign_key => :user_id,
:association_foreign_key => :blacklisted_user_id,
:insert_sql => ‘insert into blacklisted_users (user_id,
blacklisted_user_id) values (#{id}, #{record.id})’,
:extend => BlacklistedUserModule
end

module BlacklistedUserModule
def self.included(base)
puts “included”
proxy_target.instance_eval do
alias_method :old_push, :<<
remove_method :<<
end
end

def <<(*args)
puts “new push”
old_push(*args)
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs