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.
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
I’m guessing here, but this statement among other things, adds the
“userlevels” (plural) method to user. You’re trying to access
user.userlevel. (singular).
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?
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.