# Overloading and wrapping an association method

**URL:** <https://www.ruby-forum.com/t/overloading-and-wrapping-an-association-method/133767>\
**Category:** Rails\
**Created:** [April 3, 2008, 11:36am UTC](https://www.ruby-forum.com/t/overloading-and-wrapping-an-association-method/133767 "2008-04-03T11:36:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jesseproudman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jesseproudman](https://www.ruby-forum.com/u/jesseproudman)\
**Post date:** [April 3, 2008, 11:36am UTC](https://www.ruby-forum.com/t/overloading-and-wrapping-an-association-method/133767/1 "2008-04-03T11:36:56Z")

</div>

Hi all:

For example I have:

class Color \< ActiveRecord::Base  
belongs\_to :ball  
end

class Ball \< ActiveRecord::Base  
has\_many :colors  
end

What I want to do is to write a method that:

1. takes the result of Ball#colors, which is an array of Color objects
2. do something with the array
3. return the result

but I want to name my method Ball#colors, i.e. I want to overload the  
generated association method:  
class Ball \< ActiveRecord::Base  
def colors  
# do something here  
end  
end

Is there anyway to do this?

Thanks,

Jesse

---

<div class="post-metadata">

**Author:** ![jesseproudman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jesseproudman](https://www.ruby-forum.com/u/jesseproudman)\
**Post date:** [April 3, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/overloading-and-wrapping-an-association-method/133767/2 "2008-04-03T20:21:49Z")

</div>

Hmmm… I’m thinking somewhere along the lines of using Object#method  
to store the original method as a class variable, but this seems very  
hacky to me. Is there any other way?

Best regards,

Jesse

---

<div class="post-metadata">

**Author:** ![jesseproudman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jesseproudman](https://www.ruby-forum.com/u/jesseproudman)\
**Post date:** [April 4, 2008, 7:12am UTC](https://www.ruby-forum.com/t/overloading-and-wrapping-an-association-method/133767/3 "2008-04-04T07:12:21Z")

</div>

Well, you could use the “alias” method:  
[http://ruby.about.com/od/learnruby/qt/using\_alias.htm](http://ruby.about.com/od/learnruby/qt/using_alias.htm)

But really, what you originally wrote is perfectly fine. The  
has\_many :colors just creates, among other things, a getter and setter  
method. Then, by (re)defining “colors” via your “def colors…” code,  
you are overriding that.

I did a quick test and it works just fine. You could still use alias  
so you keep a reference to the assocation. For example, wrap it up in  
a single class and do this:

class Ball \< ActiveRecord::Base  
has\_many :colors  
alias :old\_colors :colors

def colors  
puts "in new colors def  
end  
end

That works just fine.

-Danimal

---

<div class="post-metadata">

**Author:** ![jesseproudman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jesseproudman](https://www.ruby-forum.com/u/jesseproudman)\
**Post date:** [April 5, 2008, 4:15am UTC](https://www.ruby-forum.com/t/overloading-and-wrapping-an-association-method/133767/4 "2008-04-05T04:15:27Z")

</div>

Hi Danimal:

Your suggestion works perfectly! Thanks 🙂

Best regards,

Jesse
