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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 4 Mar 2008, at 18:45, Grayson Piercee wrote:> > 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.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> > > Does this have anything to do with attr_reader / attr_writer? > > Thanks, > > GP > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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). -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, Mar 4, 2008 at 1:45 PM, Grayson Piercee <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---