# Non-"table\_id" foreign key

**URL:** <https://www.ruby-forum.com/t/non-table-id-foreign-key/75103>\
**Category:** Rails\
**Created:** [November 7, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/non-table-id-foreign-key/75103 "2006-11-07T16:12:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [November 7, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/non-table-id-foreign-key/75103/1 "2006-11-07T16:12:12Z")

</div>

Okay folks, I’m feeling like a fool. I’m sure I’m not the first person  
to run into this, but I can’t find the necessary info.

Basically I need to create two tables. The first to hold actual  
content. And the second to link pairs of content records. For  
example, a “persons” table and a “friends” table that pairs up two  
person records. Obviously, I can’t have two fields in the friends  
table named ‘person\_id’. So how do I assign a field as a foreign key,  
that isn’t named in the default format “table\_id”?

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [November 7, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/non-table-id-foreign-key/75103/2 "2006-11-07T16:32:15Z")

</div>

your table friends:

has  
left\_person\_id  
right\_person\_id

so  
class Friend

…

belongs\_to :right\_person, :class\_name =\> ‘Person’, :foreign\_key =\>  
‘right\_person\_id’  
belongs\_to :left\_person, :class\_name =\> ‘Person’, :foreign\_key =\>  
‘left\_person\_id’

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [November 7, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/non-table-id-foreign-key/75103/3 "2006-11-07T16:42:19Z")

</div>

check out this article and the comments to it, someone did exactly  
something like this…

[http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through](http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through)

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [November 7, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/non-table-id-foreign-key/75103/4 "2006-11-07T17:04:25Z")

</div>

igotimac, Thorsten L, & Bryan - Thank you all so much! You’ll ROCK!

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [November 7, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/non-table-id-foreign-key/75103/5 "2006-11-07T16:53:50Z")

</div>

will wrote:

> Okay folks, I’m feeling like a fool. I’m sure I’m not the first person  
> to run into this, but I can’t find the necessary info.
> 
> Basically I need to create two tables. The first to hold actual  
> content. And the second to link pairs of content records. For  
> example, a “persons” table and a “friends” table that pairs up two  
> person records. Obviously, I can’t have two fields in the friends  
> table named ‘person\_id’. So how do I assign a field as a foreign key,  
> that isn’t named in the default format “table\_id”?

You could also use a has\_and\_belongs\_to\_many association if your mapping  
table really just contains the 2 person ids.

Like igotimac implied in his comment, you can name your foreign keys  
whatever you want, just reference them explicitly when you create your  
association using the options macros for the association (foreign\_key=\>,  
class\_name=\>, etc)

Here is a reference to has\_and\_belongs\_to\_many:  
[http://wiki.rubyonrails.org/rails/pages/has\_and\_belongs\_to\_many](http://wiki.rubyonrails.org/rails/pages/has_and_belongs_to_many)
