# Noob Q: How to access a belongs\_to object

**URL:** <https://www.ruby-forum.com/t/noob-q-how-to-access-a-belongs-to-object/129263>\
**Category:** Rails\
**Created:** [February 15, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/noob-q-how-to-access-a-belongs-to-object/129263 "2008-02-15T21:26:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![studlee2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@studlee2](https://www.ruby-forum.com/u/studlee2)\
**Post date:** [February 15, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/noob-q-how-to-access-a-belongs-to-object/129263/1 "2008-02-15T21:26:12Z")

</div>

| 

Sorry for the simplicity on this.&nbsp; I hope this makes for a quick response.

&nbsp;

I have two classes:

&nbsp;

class UserProfile \< ActiveRecord::Base  
&nbsp; belongs\_to :UserGroup  
end

class UserGroup \< ActiveRecord::Base

&nbsp;&nbsp;has\_many :UserProfiles

end

&nbsp;

I want to display the UserGroup.Name for an UserProfile instance.

&nbsp;

This snippet returns the correct User\_Group id:

&nbsp;

@user\_profile.User\_Group\_id

&nbsp;

This code seemed the most logical to me:

&nbsp;

@user\_profile.UserGroup.Name

&nbsp;

but I get this error:

```
You have a nil object when you 
didn't expect it!
The error occurred while evaluating nil.Name
```

```
What 
should the call be?
```

```
Thanks in 
advance!
```

 |

  

* * *
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. [Try it now.](http://us.rd.yahoo.com/evt=51733/*http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ)  
--~--~---------~--~----~------------~-------~--~----~  
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 removed\_email\_address@domain.invalid   
 To unsubscribe from this group, send email to removed\_email\_address@domain.invalid   
 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en   
 -~----------~----~----~----~------~----~------~--~---

---

<div class="post-metadata">

**Author:** ![studlee2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@studlee2](https://www.ruby-forum.com/u/studlee2)\
**Post date:** [February 15, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/noob-q-how-to-access-a-belongs-to-object/129263/2 "2008-02-15T23:41:51Z")

</div>

FYI, I figured this out. I shouldn’t have been using capital letters  
in my classes. But now, I’ve run into a different problem.

These two classes:  
class UserProfile \< ActiveRecord::Base  
belongs\_to :UserGroup  
end

class UserGroup \< ActiveRecord::Base  
has\_many :UserProfiles  
end

become:  
class UserProfile \< ActiveRecord::Base  
belongs\_to :usergroup  
end

class UserGroup \< ActiveRecord::Base  
has\_many :userprofiles  
end

I’m not getting the error below when I make this call:

\<% for user\_profile in @user\_profiles %\>  
…

\<%=h user\_profile.usergroup.Name %\>  
…

========Error======  
uninitialized constant UserProfile::Usergroup

Thoughts?

---

<div class="post-metadata">

**Author:** ![studlee2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@studlee2](https://www.ruby-forum.com/u/studlee2)\
**Post date:** [February 16, 2008, 8:59am UTC](https://www.ruby-forum.com/t/noob-q-how-to-access-a-belongs-to-object/129263/3 "2008-02-16T08:59:17Z")

</div>

On Feb 15, 4:41 pm, stevepeters [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> become:  
> class UserProfile \< ActiveRecord::Base  
> belongs\_to :usergroup

Should be ‘user\_group’

> end
> 
> class UserGroup \< ActiveRecord::Base  
> has\_many :userprofiles

and ‘user\_profiles’

Multiple words in the class name correspond to multiple words in the  
table and relationship names. Here’s another tip to preserve sanity:  
Follow the dictionary when deciding whether a name is one word or  
multiple words. This rule tells you to call the class ‘UserGroup’  
and the table ‘user\_groups’ instead of ‘Usergroup’ and ‘usergroups’.
