# Problem with belongs\_to

**URL:** <https://www.ruby-forum.com/t/problem-with-belongs-to/71443>\
**Category:** Rails\
**Created:** [October 8, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/problem-with-belongs-to/71443 "2006-10-08T23:26:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 8, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/problem-with-belongs-to/71443/1 "2006-10-08T23:26:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 8, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/problem-with-belongs-to/71443/2 "2006-10-08T23:58:30Z")

</div>

On 10/8/06, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 9, 2006, 1:10am UTC](https://www.ruby-forum.com/t/problem-with-belongs-to/71443/3 "2006-10-09T01:10:40Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 9, 2006, 1:26am UTC](https://www.ruby-forum.com/t/problem-with-belongs-to/71443/4 "2006-10-09T01:26:36Z")

</div>

On 10/8/06, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 9, 2006, 1:20am UTC](https://www.ruby-forum.com/t/problem-with-belongs-to/71443/5 "2006-10-09T01:20:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 9, 2006, 1:28am UTC](https://www.ruby-forum.com/t/problem-with-belongs-to/71443/6 "2006-10-09T01:28:37Z")

</div>

On 10/8/06, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.
