Named collection forward fetching

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

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).

On Tue, Mar 4, 2008 at 1:45 PM, Grayson P.
[email protected] 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

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

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