# Two foreign keys to the same table

**URL:** <https://www.ruby-forum.com/t/two-foreign-keys-to-the-same-table/53361>\
**Category:** Rails\
**Created:** [February 2, 2006, 11:03am UTC](https://www.ruby-forum.com/t/two-foreign-keys-to-the-same-table/53361 "2006-02-02T11:03:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@Matias](https://www.ruby-forum.com/u/Matias)\
**Post date:** [February 2, 2006, 11:03am UTC](https://www.ruby-forum.com/t/two-foreign-keys-to-the-same-table/53361/1 "2006-02-02T11:03:07Z")

</div>

Hi,

I’ve two tables:

ProductLists (id, name, …)  
Product (id,name,product\_list\_1\_id, product\_list\_2\_id, …)

How can I map the fields product\_list\_1\_id and product\_list\_2\_id to the  
ProductLists table in activerecord???

Thanks.

---

<div class="post-metadata">

**Author:** ![Matias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@Matias](https://www.ruby-forum.com/u/Matias)\
**Post date:** [February 2, 2006, 11:25am UTC](https://www.ruby-forum.com/t/two-foreign-keys-to-the-same-table/53361/2 "2006-02-02T11:25:50Z")

</div>

Matias wrote:

> Hi,
> 
> I’ve two tables:
> 
> ProductLists (id, name, …)  
> Product (id,name,product\_list\_1\_id, product\_list\_2\_id, …)
> 
> How can I map the fields product\_list\_1\_id and product\_list\_2\_id to the  
> ProductLists table in activerecord???
> 
> Thanks.

class Product \< blabla  
belongs\_to  
:list1,:class\_name=\>“ProductList”,:foreign\_key=\>“product\_list\_1\_id”  
belongs\_to  
:list2,:class\_name=\>“ProductList”,:foreign\_key=\>“product\_list\_2\_id”

end

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000473)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.
