# Overwriting / Decorating ActiveRecord association accessor

**URL:** <https://www.ruby-forum.com/t/overwriting-decorating-activerecord-association-accessor/153647>\
**Category:** Rails\
**Created:** [November 19, 2008, 7:54pm UTC](https://www.ruby-forum.com/t/overwriting-decorating-activerecord-association-accessor/153647 "2008-11-19T19:54:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@Marcel\_H](https://www.ruby-forum.com/u/Marcel_H)\
**Post date:** [November 19, 2008, 7:54pm UTC](https://www.ruby-forum.com/t/overwriting-decorating-activerecord-association-accessor/153647/1 "2008-11-19T19:54:32Z")

</div>

Hi,  
I am trying to overwrite the accessor of a has\_many association.  
Basically my class looks like this:

class Person  
has\_many :emails  
has\_one :link, :class\_name =\> ‘PersonProxy’

def emails\_with\_link  
link.emails + emails\_without\_link  
end  
alias\_method\_chain :emails, :link  
end

Now that works fine if I only access the collection like

> > Person.find(1).emails  
> > =\> [#\<Email…]

But using any collection specific methods throw a NoMethodError

> > Person.find(1).emails.build  
> > =\> NoMethodError: undefined method `build’ for #Array:0x226c634

which makes sense, as I return an array…

But how do I return the Association? Or are there any other tricks to  
implement this decorator for associations?

Cheers  
Marcel

---

<div class="post-metadata">

**Author:** ![Marcel\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@Marcel\_H](https://www.ruby-forum.com/u/Marcel_H)\
**Post date:** [November 19, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/overwriting-decorating-activerecord-association-accessor/153647/2 "2008-11-19T20:21:43Z")

</div>

On Nov 19, 6:08 pm, Marcel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> which makes sense, as I return an array…

The trick is that link.emails isn’t an array - it’s an association  
proxy.  
I’m not sure what the write thing to do here is. Maybe something like

has\_many :emails do  
def load\_target  
super  
proxy\_target += proxy\_owner.link.emails  
end  
end

might do the trick ?

Fred

---

<div class="post-metadata">

**Author:** ![Marcel\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@Marcel\_H](https://www.ruby-forum.com/u/Marcel_H)\
**Post date:** [November 20, 2008, 1:23pm UTC](https://www.ruby-forum.com/t/overwriting-decorating-activerecord-association-accessor/153647/3 "2008-11-20T13:23:18Z")

</div>

> The trick is that link.emails isn’t an array - it’s an association  
> proxy.  
> I’m not sure what the write thing to do here is. Maybe something like

Thanks so much for your input. You got me in the right direction:

has\_many :emails do  
def load\_target  
super  
if !loaded? and proxy\_owner.link.respond\_to?(:emails)  
@target += proxy\_owner.link.emails  
end  
@target  
end  
end

This works like a charm 🙂 Thumbs up!

Marcel

---

<div class="post-metadata">

**Author:** ![Marcel\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@Marcel\_H](https://www.ruby-forum.com/u/Marcel_H)\
**Post date:** [November 20, 2008, 1:38pm UTC](https://www.ruby-forum.com/t/overwriting-decorating-activerecord-association-accessor/153647/4 "2008-11-20T13:38:39Z")

</div>

I was a bit too fast and euphoric 🙂  
Now this works better:

has\_many :emails do  
def load\_target  
super  
if not @loaded\_link and proxy\_owner.link.respond\_to?(:emails)  
puts “loading link”  
@target += proxy\_owner.link.emails  
@loaded\_link = true  
end  
@target  
end  
def reset  
super  
@loaded\_link = false  
end  
end
