# Multiple associations to the same class

**URL:** <https://www.ruby-forum.com/t/multiple-associations-to-the-same-class/53781>\
**Category:** Rails\
**Created:** [February 14, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/multiple-associations-to-the-same-class/53781 "2006-02-14T15:13:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Douglas\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Douglas\_T](https://www.ruby-forum.com/u/Douglas_T)\
**Post date:** [February 14, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/multiple-associations-to-the-same-class/53781/1 "2006-02-14T15:13:09Z")

</div>

Hi,

I cannot seem to create associations like this:

class Project \< ActiveRecord::Base  
belongs\_to :manager, :class\_name =\> ‘User’, :foreign\_key =\> ‘manager’  
belongs\_to :liaison, :class\_name =\> ‘User’, :foreign\_key =\> ‘liaison’  
end

p = Project.new  
p.manager  
=\> 1

trying to retrieve associated objects doesn’t work and I only receive  
the  
associated record’s ID value as a Fixnum instance. Is there any way to  
implement this particular association?

---

<div class="post-metadata">

**Author:** ![Douglas\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Douglas\_T](https://www.ruby-forum.com/u/Douglas_T)\
**Post date:** [February 14, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/multiple-associations-to-the-same-class/53781/2 "2006-02-14T19:27:42Z")

</div>

I guess you should name you association different to the table’s field  
name.

Kent.

---

<div class="post-metadata">

**Author:** ![Douglas\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Douglas\_T](https://www.ruby-forum.com/u/Douglas_T)\
**Post date:** [February 14, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/multiple-associations-to-the-same-class/53781/3 "2006-02-14T19:39:48Z")

</div>

sorry I mispasted my User class:

class User \< ActiveRecord::Base  
  
has\_many :bugs\_opened, :class\_name =\> “Bug”, :foreign\_key =\> “opened\_by”

has\_many :bugs\_assigned, :class\_name =\> “Bug”, :foreign\_key =\>  
“assigned\_to”

---

<div class="post-metadata">

**Author:** ![Douglas\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Douglas\_T](https://www.ruby-forum.com/u/Douglas_T)\
**Post date:** [February 14, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/multiple-associations-to-the-same-class/53781/4 "2006-02-14T19:36:47Z")

</div>

On 2/14/06, Kent S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess you should name you association different to the table’s field name.

Exactly right, that’s how I got this sort of setup running. You should  
try

belongs\_to :manager\_user, :class\_name =\> ‘User’, :foreign\_key =\>  
‘manager’  
belongs\_to :liaison\_user, :class\_name =\> ‘User’, :foreign\_key =\>  
‘liaison’

or somesuch. Then call project.manager\_user when you want the user.

Either that or rename your “manager” column to “manager\_id” in the db.

In my class it’s like:

class Bug \< ActiveRecord::Base  
  
belongs\_to :opened\_by\_user, :class\_name =\> “User”, :foreign\_key =\>  
“opened\_by”

belongs\_to :assigned\_to\_user, :class\_name =\> “User”, :foreign\_key =\>  
“assigned\_to”

end

class Bug \< ActiveRecord::Base  
  
belongs\_to :opened\_by\_user, :class\_name =\> “User”, :foreign\_key =\>  
“opened\_by”

belongs\_to :assigned\_to\_user, :class\_name =\> “User”, :foreign\_key =\>  
“assigned\_to”

end

---

<div class="post-metadata">

**Author:** ![Douglas\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Douglas\_T](https://www.ruby-forum.com/u/Douglas_T)\
**Post date:** [February 15, 2006, 4:19am UTC](https://www.ruby-forum.com/t/multiple-associations-to-the-same-class/53781/5 "2006-02-15T04:19:57Z")

</div>

hmm, I got it to work by appending ‘\_id’ to the :foreign\_key values,  
like  
so: ‘manager\_id’

---

<div class="post-metadata">

**Author:** ![Douglas\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Douglas\_T](https://www.ruby-forum.com/u/Douglas_T)\
**Post date:** [March 22, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/multiple-associations-to-the-same-class/53781/6 "2007-03-22T12:28:14Z")

</div>

> I imagine you don’t then need :foreign\_key =\> ‘manager\_id’ I bet RoR  
> will make that association for you
> 
> \_T

I wish; it took my hours to find you NEED to define foreign\_key if you  
use class\_name

---

<div class="post-metadata">

**Author:** ![Douglas\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Douglas\_T](https://www.ruby-forum.com/u/Douglas_T)\
**Post date:** [February 15, 2006, 4:58pm UTC](https://www.ruby-forum.com/t/multiple-associations-to-the-same-class/53781/7 "2006-02-15T16:58:41Z")

</div>

Douglas T. wrote:

> hmm, I got it to work by appending ‘\_id’ to the :foreign\_key values,  
> like  
> so: ‘manager\_id’

I imagine you don’t then need :foreign\_key =\> ‘manager\_id’ I bet RoR  
will make that association for you

\_T
