# Create 2 fk referencing the same table

**URL:** <https://www.ruby-forum.com/t/create-2-fk-referencing-the-same-table/106379>\
**Category:** Rails\
**Created:** [June 9, 2007, 11:43am UTC](https://www.ruby-forum.com/t/create-2-fk-referencing-the-same-table/106379 "2007-06-09T11:43:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2007, 11:43am UTC](https://www.ruby-forum.com/t/create-2-fk-referencing-the-same-table/106379/1 "2007-06-09T11:43:32Z")

</div>

Hi everyone! I have a problem with defining 2 fk referencing the same  
table. I have a Program table and a Team table. The Program should  
have an away team and a home team fk. From my understanding, “the fk  
column should be named after the class of the target table, converted  
to lowercase, with \_id appended”. But, in my case, I have 2 fk  
referencing the same table. How can I do this?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/create-2-fk-referencing-the-same-table/106379/2 "2007-06-09T12:41:38Z")

</div>

On 6/9/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi everyone! I have a problem with defining 2 fk referencing the same  
> table. I have a Program table and a Team table. The Program should  
> have an away team and a home team fk. From my understanding, “the fk  
> column should be named after the class of the target table, converted  
> to lowercase, with \_id appended”. But, in my case, I have 2 fk  
> referencing the same table. How can I do this?

I think this might work, but I didn’t set up a DB or an app to test it

class Program  
belongs\_to :team, :foreign\_key =\> “home\_team\_id”  
belongs\_to :team, :foreign\_key =\> “away\_team\_id”  
end

–  
F. Morgan Whitney

> **[Home - Blizzo](http://www.blizzo.com)**
>
> Insight. Culture. Passion. OUR STORY About Our Clients We recognize that our clients are the key element in our success and that we play an important part in helping build their business and brand. Whether our client is a small service business or a...

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/create-2-fk-referencing-the-same-table/106379/3 "2007-06-09T12:56:42Z")

</div>

Morgan Whitney wrote the following on 09.06.2007 12:41 :

> I think this might work, but I didn’t set up a DB or an app to test it
> 
> class Program  
> belongs\_to :team, :foreign\_key =\> “home\_team\_id”  
> belongs\_to :team, :foreign\_key =\> “away\_team\_id”  
> end

class Program  
belongs\_to :home\_team, :class\_name =\> :team  
belongs\_to :away\_team, :class\_name =\> :team  
end

To avoid mistakes, just remember that

belongs\_to

creates the:

model., model.=, …

accessors so you obviously don’t want several “belongs\_to …”  
with identical values of .

(Note that this memento is mostly valid for other kinds of associations)

The foreign key should be computed automatically (by adding \_id in this  
example), if it doesn’t suits you, then you can use the :foreign\_key  
parameter.

See ActiveRecord docs for more.

Lionel.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/create-2-fk-referencing-the-same-table/106379/4 "2007-06-09T12:57:42Z")

</div>

Lionel B. wrote the following on 09.06.2007 12:55 :

> > > referencing the same table. How can I do this?
> 
> class Program  
> belongs\_to :home\_team, :class\_name =\> :team

better be :class\_name =\> “Team”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/create-2-fk-referencing-the-same-table/106379/5 "2007-06-09T13:28:48Z")

</div>

Thanks guys… I will try it out and get back to you…

On Jun 9, 1:56 pm, Lionel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/create-2-fk-referencing-the-same-table/106379/6 "2007-06-09T21:51:43Z")

</div>

Hi, guys! I’m back…Thanks for your feedback…it worked …great  
advices from all of you…

On Jun 9, 8:28 pm, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/create-2-fk-referencing-the-same-table/106379/7 "2007-06-09T19:28:56Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi everyone! I have a problem with defining 2 fk referencing the same  
> table. I have a Program table and a Team table. The Program should  
> have an away team and a home team fk. From my understanding, “the fk  
> column should be named after the class of the target table, converted  
> to lowercase, with \_id appended”. But, in my case, I have 2 fk  
> referencing the same table. How can I do this?

For non-standard association names, you have to specify both the foreign  
key and the class name. Try this:

class Program  
belongs\_to :home\_team,  
:class\_name =\> “Team”, :foreign\_key =\> “home\_team\_id”  
belongs\_to :away\_team,  
:class\_name =\> “Team”, :foreign\_key =\> “away\_team\_id”  
end

class Team  
has\_many :home\_programs,  
:class\_name =\> “Program”, :foreign\_key =\> “home\_team\_id”  
has\_many :away\_programs,  
:class\_name =\> “Program”, :foreign\_key =\> “away\_team\_id”  
end

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
