# How to override association proxy push()

**URL:** <https://www.ruby-forum.com/t/how-to-override-association-proxy-push/114251>\
**Category:** Rails\
**Created:** [September 10, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/how-to-override-association-proxy-push/114251 "2007-09-10T16:14:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [September 10, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/how-to-override-association-proxy-push/114251/1 "2007-09-10T16:14:36Z")

</div>

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
