I''m obviously missing something simple. I have two models containing the following: class Userlevel <ActiveRecord::Base has_many :users --- class User < ActiveRecord::Base --- belongs_to :userlevels --- userlevels is a table with id as the primary key. The users table has a column userlevel_id that links it to the userlevels table. In my controller, I have @users = User.find(:all) In my view I have <%= for user in @users %> <%= user.userlevel.usertype %> At this point I get a "NoMethodError: undefined method `userlevel'' for #<User:0xb753abd0>". I''m looking at the example in Agile Web Development, and my code looks right to me. Why can''t I get the associated data from userlevels table? What am I missing? It''s probably obvious to the guru''s here. -- 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 -~----------~----~----~----~------~----~------~--~---
On 10/8/06, Michael Satterwhite <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m obviously missing something simple. I have two models containing the > following: > > class Userlevel <ActiveRecord::Base > has_many :users > --- > > class User < ActiveRecord::Base > --- > belongs_to :userlevelsI''m guessing here, but this statement among other things, adds the "userlevels" (plural) method to user. You''re trying to access user.userlevel. (singular). Try this: ruby script\console p User.public_methods.grep(/^user/) and see what methods are out there. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Try this: > > ruby script\console > > p User.public_methods.grep(/^user/) > > and see what methods are out there.That was a good idea, but the mystery broadens. There are no methods in User starting with "user". Why? -- 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 -~----------~----~----~----~------~----~------~--~---
Michael Campbell wrote:> > Try this: > > ruby script\console > > p User.public_methods.grep(/^user/) > > and see what methods are out there.Found it! (kinda) It was the plurality of the words as you suggested, but I''m not sure I''m understanding why. The table names in the db are users and userlevels. The class names are User and Userlevel. The switching between singular and plural forms that rails does isn''t obvious to me as to when. Why is has_many (and belongs_to) not referring to the table name? Thanks for the help ---Michael -- 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 -~----------~----~----~----~------~----~------~--~---
On 10/8/06, Michael Satterwhite <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > Try this: > > > > ruby script\console > > > > p User.public_methods.grep(/^user/) > > > > and see what methods are out there. > > That was a good idea, but the mystery broadens. There are no methods in > User starting with "user". Why?There might be some magic going on with rails and "method_missing"; that is, some metaprogramming. I''m way out of my league here though, I''m afraid. Poke around in the console and see; I suspect User#userlevelsprobably does exist if you try to execute it in a rails app. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/8/06, Michael Satterwhite <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Michael Campbell wrote: > > > > > Try this: > > > > ruby script\console > > > > p User.public_methods.grep(/^user/) > > > > and see what methods are out there. > > Found it! (kinda) > > It was the plurality of the words as you suggested, but I''m not sure I''m > understanding why. > > The table names in the db are users and userlevels. The class names are > User and Userlevel. The switching between singular and plural forms that > rails does isn''t obvious to me as to when. Why is has_many (and > belongs_to) not referring to the table name?You have many "things", but those things belong to one other "thing". It''s supposed to be easier to read; the jury''s out with me on that. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---