Problem with belongs_to

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.

On 10/8/06, Michael S. [email protected]
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 :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).

Try this:

ruby script\console

p User.public_methods.grep(/^user/)

and see what methods are out there.

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?

On 10/8/06, Michael S. [email protected]
wrote:

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#userlevels=
probably does exist if you try to execute it in a rails app.

Michael C. 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

On 10/8/06, Michael S. [email protected]
wrote:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs