# Method on a collection in a model

**URL:** <https://www.ruby-forum.com/t/method-on-a-collection-in-a-model/126401>\
**Category:** Rails\
**Created:** [January 18, 2008, 12:21am UTC](https://www.ruby-forum.com/t/method-on-a-collection-in-a-model/126401 "2008-01-18T00:21:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 18, 2008, 12:21am UTC](https://www.ruby-forum.com/t/method-on-a-collection-in-a-model/126401/1 "2008-01-18T00:21:53Z")

</div>

I have a model (x) that has\_many y. Is it possible to write a method  
that says:  
x.ys.some\_method  
I’m assuming you’d put it in the x class…but I’m not too sure where to  
go from there.

TIA

---

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 18, 2008, 12:34am UTC](https://www.ruby-forum.com/t/method-on-a-collection-in-a-model/126401/2 "2008-01-18T00:34:51Z")

</div>

On 17 Jan 2008, at 23:21, Luke G. wrote:

> I have a model (x) that has\_many y. Is it possible to write a method  
> that says:  
> x.ys.some\_method  
> I’m assuming you’d put it in the x class…but I’m not too sure  
> where to  
> go from there.

There are 2 things that you might be interested in:

Association extensions, in a nutshell:  
has\_many :ys do  
def some\_method  
end  
end  
You get some magic variables such as the owner of the association etc…

The other thing to note is that if you have

class Foo \< ActiveRecord::Base  
def self.some\_method  
end  
end

and X has\_many :foos, then you can call x.foos.some\_method and  
some\_method will magically be scoped for you.

Fred

---

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 18, 2008, 12:48am UTC](https://www.ruby-forum.com/t/method-on-a-collection-in-a-model/126401/3 "2008-01-18T00:48:28Z")

</div>

Frederick C. wrote:

> On 17 Jan 2008, at 23:21, Luke G. wrote:
> 
> Association extensions, in a nutshell:  
> has\_many :ys do  
> def some\_method  
> end  
> end

This looks to be the most attractive option. In this method I want to  
access each object in the collection though. How do you do that using  
the above method. The only examples I’ve been able to find so far have  
been to do with extending finder methods.

> You get some magic variables such as the owner of the association etc…
> 
> The other thing to note is that if you have
> 
> class Foo \< ActiveRecord::Base  
> def self.some\_method  
> end  
> end
> 
> and X has\_many :foos, then you can call x.foos.some\_method and  
> some\_method will magically be scoped for you.

This method didn’t exactly work as it was calling the method against  
class. But I really want to do something against the objects within the  
collection.

---

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 18, 2008, 12:54am UTC](https://www.ruby-forum.com/t/method-on-a-collection-in-a-model/126401/4 "2008-01-18T00:54:08Z")

</div>

On 17 Jan 2008, at 23:48, Luke G. wrote:

> This looks to be the most attractive option. In this method I want to  
> access each object in the collection though. How do you do that using  
> the above method. The only examples I’ve been able to find so far have  
> been to do with extending finder methods.

proxy\_target gets you the target object.

Fred

---

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 18, 2008, 2:03am UTC](https://www.ruby-forum.com/t/method-on-a-collection-in-a-model/126401/5 "2008-01-18T02:03:34Z")

</div>

Frederick C. wrote:

> On 17 Jan 2008, at 23:48, Luke G. wrote:
> 
> > This looks to be the most attractive option. In this method I want to  
> > access each object in the collection though. How do you do that using  
> > the above method. The only examples I’ve been able to find so far have  
> > been to do with extending finder methods.
> 
> proxy\_target gets you the target object.
> 
> Fred

Thats perfect.  
So what I ended up doing was  
in the model for x I had  
has\_many y do  
proxy\_target.collect { |n| n.to\_z }  
end

in the model for y, I had a method called to\_z.

This method of doing things seems a bit messy since I’m declaring a  
method within the has\_many. Is there a neater way to do it?
