# has\_many belongs\_to foreign key question

**URL:** <https://discuss.rubyonrails.org/t/has-many-belongs-to-foreign-key-question/30819>\
**Category:** rubyonrails-talk\
**Created:** [June 19, 2008, 1:53pm UTC](https://discuss.rubyonrails.org/t/has-many-belongs-to-foreign-key-question/30819 "2008-06-19T13:53:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 19, 2008, 1:53pm UTC](https://discuss.rubyonrails.org/t/has-many-belongs-to-foreign-key-question/30819/1 "2008-06-19T13:53:03Z")

</div>

i have a 2 tables

ITEMS id name attribute\_id attribute\_secondary\_id

ATTRIBUTES id name

what i am trying to do is have attribute\_id and attribute\_secondary\_id both link to the ATTRIBUTES tables. At the moment attribute\_id works fine but I'm not sure how i should do the attribute\_secondary\_id.

my models

ITEMS belongs\_to :attribute belongs\_to :attribute, :foreign\_key =\> 'attribute\_secondary\_id' (won't work)

ATTRIBUTES has\_many :items

i want to do something like the following

item.attribute.name (for primary attribute) item.attribute\_secondary\_id.name (not working...how should i do this?)

i don't really want to set up another table ITEM\_ATTRIBUTES. Is there a way to do this as i mentioned?

---

<div class="post-metadata">

**Author:** ![Jeff1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jeff1/32/1170_2.png) [@Jeff1](https://discuss.rubyonrails.org/u/Jeff1)\
**Post date:** [June 19, 2008, 2:12pm UTC](https://discuss.rubyonrails.org/t/has-many-belongs-to-foreign-key-question/30819/2 "2008-06-19T14:12:13Z")

</div>

You're close, but you just need to change the first parameter so you can distinguish between the two:

belongs\_to :attribute belongs\_to :secondary, :foreign\_key =\> 'attribute\_secondary\_id'

The you can do something like this:

item = Item.find(1) puts item.attribute puts item.secondary # uses attribute\_secondary\_id column to find the associated item

This should work for you (or let me know if I misunderstood the question.)

Jeff www.purpleworkshops.com www.softiesonrails.com

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [June 19, 2008, 2:33pm UTC](https://discuss.rubyonrails.org/t/has-many-belongs-to-foreign-key-question/30819/3 "2008-06-19T14:33:40Z")

</div>

> i have a 2 tables
> 
> ITEMS id name attribute\_id attribute\_secondary\_id
> 
> ATTRIBUTES id name
> 
> what i am trying to do is have attribute\_id and attribute\_secondary\_id both link to the ATTRIBUTES tables. At the moment attribute\_id works fine but I'm not sure how i should do the attribute\_secondary\_id.

I gave an example of this at [Creating multiple associations with the same table - Space Vatican](http://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table)

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 19, 2008, 3:48pm UTC](https://discuss.rubyonrails.org/t/has-many-belongs-to-foreign-key-question/30819/4 "2008-06-19T15:48:07Z")

</div>

Thanks everyone! I got it working:

&nbsp;&nbsp;&nbsp;&nbsp;belongs\_to :attribute\_primary, :class\_name =\> 'Attribute', :foreign\_key =\> 'attribute\_id' &nbsp;&nbsp;&nbsp;&nbsp;belongs\_to :attribute\_secondary, :class\_name =\> 'Attribute', :foreign\_key =\> 'attribute\_secondary\_id'
