# Has\_many belongs\_to foreign key question

**URL:** <https://www.ruby-forum.com/t/has-many-belongs-to-foreign-key-question/140894>\
**Category:** Rails\
**Created:** [June 19, 2008, 3:53pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-foreign-key-question/140894 "2008-06-19T15:53:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 19, 2008, 3:53pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-foreign-key-question/140894/1 "2008-06-19T15: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:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 19, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-foreign-key-question/140894/2 "2008-06-19T16:14:54Z")

</div>

On Jun 19, 8:53 am, Scott K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

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](http://www.purpleworkshops.com)

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 19, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-foreign-key-question/140894/3 "2008-06-19T16:34:42Z")

</div>

On 19 Jun 2008, at 14:53, Scott K. wrote:

> 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](https://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table/)**
>
> Rails makes setting up your associations dead easy and very concise. There’s a whole bunch of options that can be set however as long as you …

Fred

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 19, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-foreign-key-question/140894/4 "2008-06-19T17:48:07Z")

</div>

Thanks everyone! I got it working:

```
belongs_to :attribute_primary, :class_name => 'Attribute', 

```

:foreign\_key =\> ‘attribute\_id’  
belongs\_to :attribute\_secondary, :class\_name =\> ‘Attribute’,  
:foreign\_key =\> ‘attribute\_secondary\_id’
