Named collection forward fetching

Grayson Piercee 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 Bush wrote:

Grayson Piercee 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