# How can I overwrite the parent.children.push(child) Method?

**URL:** <https://www.ruby-forum.com/t/how-can-i-overwrite-the-parent-children-push-child-method/99554>\
**Category:** Rails\
**Created:** [January 31, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/how-can-i-overwrite-the-parent-children-push-child-method/99554 "2006-01-31T20:51:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 31, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/how-can-i-overwrite-the-parent-children-push-child-method/99554/1 "2006-01-31T20:51:42Z")

</div>

Hi all

I want to overwrite the push method (which is an alias of \<\<, the same  
as concat) of collections, and there I want to test if :uniq is set to  
true in the relationship. If so, the method should check if the passed  
object is already related to the parent, or not (only then it will be  
added).

But I just can’t find the original code of this method, so I could  
overwrite it… Can anyone tell me? Thanks. 🙂

Greets  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 1, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/how-can-i-overwrite-the-parent-children-push-child-method/99554/2 "2006-02-01T13:49:52Z")

</div>

Joshua M. wrote:

> Hi all
> 
> I want to overwrite the push method (which is an alias of \<\<, the same  
> as concat) of collections, and there I want to test if :uniq is set to  
> true in the relationship. If so, the method should check if the passed  
> object is already related to the parent, or not (only then it will be  
> added).
> 
> But I just can’t find the original code of this method, so I could  
> overwrite it… Can anyone tell me? Thanks. 🙂

activerecord-1.13.2\lib\active\_record\associations\association\_collection.rb

has:

module ActiveRecord  
module Associations  
class AssociationCollection \< AssociationProxy #:nodoc:  
…  
def \<\<(\*records)  
result = true  
load\_target  
@owner.transaction do  
flatten\_deeper(records).each do |record|  
raise\_on\_type\_mismatch(record)  
callback(:before\_add, record)  
result &&= insert\_record(record) unless @owner.new\_record?  
@target \<\< record  
callback(:after\_add, record)  
end  
end

```
     result and self
   end

   alias_method :push, :<<

```

But I’d think you’re better off not overriding that. Better to have a  
wrapper method in the parent class.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 1, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/how-can-i-overwrite-the-parent-children-push-child-method/99554/3 "2006-02-01T18:15:13Z")

</div>

Thanks a lot. But I don’t really get what to do now. 😉

module ActiveRecord  
module Associations  
class AssociationCollection \< AssociationProxy #:nodoc:  
def push\_uniq(record)  
# what to do here?  
end  
end  
end  
end

I’m quite confused with Rails’ internal codes… Where do I get the  
already associated objects’ array from so I can check if the param  
record is already incliuded in it?  
Btw at the moment I only get a

ArgumentError: wrong number of arguments (1 for 0)

when I use Members.find(1).disc\_jockeys.push\_uniq(DiscJockey.find(1))

Why that?
