# Association

**URL:** <https://www.ruby-forum.com/t/association/153194>\
**Category:** Rails\
**Created:** [November 13, 2008, 7:40am UTC](https://www.ruby-forum.com/t/association/153194 "2008-11-13T07:40:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 13, 2008, 7:40am UTC](https://www.ruby-forum.com/t/association/153194/1 "2008-11-13T07:40:09Z")

</div>

Hi…  
I would like to know what i ve done wrong in my association.  
I have 4 Models.  
user\_info  
company\_info  
department  
role

Actually i want to get Particular user’s company,and which department he  
belongs and what role he has…  
These inforamtions i would like to get.  
So i made association.

class CompanyInfo \< ActiveRecord::Base  
#-------------------------------------  
has\_one :user\_info

class UserInfo \< ActiveRecord::Base

```
belongs_to :company_info
belongs_to :department
belongs_to :role

```

#-------------------------------------  
class Department \< ActiveRecord::Base

```
has_one :user_info

```

#-------------------------------------  
class Role \< ActiveRecord::Base

```
has_one :user_info

```

#-------------------------------------  
Now i can able to get department information for a paricular user and  
role inforamtion for a particular user.  
But i m unable to get Company information for a particular user.  
What would be the Problem in my case.  
Did i made anything wrong.  
Pls help me.

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 13, 2008, 11:35am UTC](https://www.ruby-forum.com/t/association/153194/2 "2008-11-13T11:35:14Z")

</div>

The setup seems to be right, assuming  
there is a company\_info\_id in the user\_info table.

Are the records in the database ok and have the  
right id?  
What code do you use to get the CompanyInfo?  
Something like:

@user\_info = UserInfo(:first)

That should allow to acces it like that:  
@user\_info.company\_info

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 13, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/association/153194/3 "2008-11-13T16:20:03Z")

</div>

It seems all of your “has\_one” should be has\_many. A company, role and  
department should have many users. That might reduce problems with  
your associations.

On Nov 13, 2:40 pm, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
