# Named collection forward fetching

**URL:** <https://www.ruby-forum.com/t/named-collection-forward-fetching/130955>\
**Category:** Rails\
**Created:** [March 4, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/named-collection-forward-fetching/130955 "2008-03-04T19:45:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [March 4, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/named-collection-forward-fetching/130955/1 "2008-03-04T19:45:53Z")

</div>

Hello,

I’m getting an “NoMethodError” traversing a named collection. Let me  
explain

I have a named collection called best\_friend in a class called user

def self.best\_friend  
find :all, :conditions =\> “designation = ‘Best Friend’”  
end

and if i do current\_user.best\_friend everything works great, I can pull  
up the user’s name as an example. My user model has a one-to-many  
relationship with interests. If I do current\_user.best\_friend.interests  
however it gives a no method error.

Does this have anything to do with attr\_reader / attr\_writer?

Thanks,

GP

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [March 4, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/named-collection-forward-fetching/130955/2 "2008-03-04T20:29:46Z")

</div>

Grayson P. wrote:

> def self.best\_friend  
> find :all, :conditions =\> “designation = ‘Best Friend’”  
> end
> 
> and if i do current\_user.best\_friend everything works great, I can pull  
> up the user’s name as an example. My user model has a one-to-many  
> relationship with interests. If I do current\_user.best\_friend.interests  
> however it gives a no method error.

The result of a “find :all” is an array, so your #best\_friend method  
returns an array which doesn’t respond (normally!) to #interests. If  
you change to “find :first” then this will work (except when there is no  
best friend, in which case #best\_friend would return nil and you’d get  
an error trying to call #interests on nil).

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [March 5, 2008, 4:10am UTC](https://www.ruby-forum.com/t/named-collection-forward-fetching/130955/3 "2008-03-05T04:10:37Z")

</div>

On Tue, Mar 4, 2008 at 1:45 PM, Grayson P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> and if i do current\_user.best\_friend everything works great, I can pull  
> up the user’s name as an example. My user model has a one-to-many  
> relationship with interests. If I do current\_user.best\_friend.interests  
> however it gives a no method error.

you’ve defined a class level method. You’re calling it on an instance  
of your class. This shouldn’t even work.

say you’ve got a typo and it’s actually defined as an instance method  
and you’re trying current\_user.best\_friend, you’re going to get an  
array of values returned. Of course the array has no ‘interests’  
method, since that’s defined in your user model. I think what you may  
want is something like the following:

def best\_friend  
User.find :first, :conditions =\> [‘designation = ?’, ‘Best Friend’]  
end

but… looking back on your original method, it doesn’t really make  
sense to me. You’ve got:

def self.best\_friend  
find :all, :conditions =\> “designation = ‘Best Friend’”  
end

so you want to find all the users who are designated as a ‘Best  
Friend’. But a best friend to whom? Say you’ve got 10 users and each  
of them has ‘Best Friend’ set, so that means everyone is a best friend  
to everyone else, which is probably not what you want.

I think what you want is either a self referential join, so each user  
can have an arbitrary number of best friends, or give each user a  
‘best\_friend\_id’ and use a has\_one :best\_friend, :class =\> ‘User’  
association if you only want each user to have a single best friend.

Mike

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [March 5, 2008, 1:01pm UTC](https://www.ruby-forum.com/t/named-collection-forward-fetching/130955/4 "2008-03-05T13:01:33Z")

</div>

On 4 Mar 2008, at 18:45, Grayson P. wrote:

> end
> 
> and if i do current\_user.best\_friend everything works great, I can  
> pull  
> up the user’s name as an example. My user model has a one-to-many  
> relationship with interests. If I do  
> current\_user.best\_friend.interests  
> however it gives a no method error.  
> Of course: your best\_friend method returns an array (bit odd that a  
> method return a collection is a singular). That array doesn’t have an  
> interests method, though its members do.  
> ALso from the code you’ve posted you’ve made best\_friend a class  
> method rather than an instance method.  
> Finally, why not use an association for this?

Fred

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [March 5, 2008, 1:45am UTC](https://www.ruby-forum.com/t/named-collection-forward-fetching/130955/5 "2008-03-05T01:45:54Z")

</div>

Mark B. wrote:

> Grayson P. wrote:
> 
> > def self.best\_friend  
> > find :all, :conditions =\> “designation = ‘Best Friend’”  
> > end
> > 
> > and if i do current\_user.best\_friend everything works great, I can pull  
> > up the user’s name as an example. My user model has a one-to-many  
> > relationship with interests. If I do current\_user.best\_friend.interests  
> > however it gives a no method error.
> 
> The result of a “find :all” is an array, so your #best\_friend method  
> returns an array which doesn’t respond (normally!) to #interests. If  
> you change to “find :first” then this will work (except when there is no  
> best friend, in which case #best\_friend would return nil and you’d get  
> an error trying to call #interests on nil).

Mark,

Thank you. That fixed it. It’s always the simple things that get you.

GP
