# Type with two references to the same table

**URL:** <https://www.ruby-forum.com/t/type-with-two-references-to-the-same-table/134333>\
**Category:** Rails\
**Created:** [April 9, 2008, 10:06pm UTC](https://www.ruby-forum.com/t/type-with-two-references-to-the-same-table/134333 "2008-04-09T22:06:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![toddlc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e79b87/32.png) [@toddlc](https://www.ruby-forum.com/u/toddlc)\
**Post date:** [April 9, 2008, 10:06pm UTC](https://www.ruby-forum.com/t/type-with-two-references-to-the-same-table/134333/1 "2008-04-09T22:06:32Z")

</div>

Hi all,

Struggling with the case of one type with two refs to another type:

Type  
has\_many :attributes

Attribute  
belongs\_to :owner, :foreign\_key =\> “owner\_id”, :class\_name = “Type”  
has\_one :type

When I

type.attributes \<\< attribute

it goes in the owner\_id column.

I can’t find any examples of this on the web, looking for any hints.

Thanks,

Todd

---

<div class="post-metadata">

**Author:** ![toddlc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e79b87/32.png) [@toddlc](https://www.ruby-forum.com/u/toddlc)\
**Post date:** [April 9, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/type-with-two-references-to-the-same-table/134333/2 "2008-04-09T22:10:39Z")

</div>

On Apr 9, 4:06 pm, Todd C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When I
> 
> type.attributes \<\< attribute
> 
> it goes in the owner\_id column.

!!! This should say: “it goes in the type\_id  
column” !!!

---

<div class="post-metadata">

**Author:** ![toddlc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e79b87/32.png) [@toddlc](https://www.ruby-forum.com/u/toddlc)\
**Post date:** [April 9, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/type-with-two-references-to-the-same-table/134333/3 "2008-04-09T22:30:01Z")

</div>

Todd C. wrote:

> !!! This should say: “it goes in the type\_id  
> column” !!!

You need to tell the has\_many relationship about the foreign key, too:

has\_many :attributes, :foreign\_key =\> :owner\_id

It’s one of the quirks of Rails, unfortunately.

Also, I’d be wary of using “Type” for anything to do with an  
ActiveRecord model. If you start getting strange errors, then have a  
look here:

[http://wiki.rubyonrails.org/rails/pages/ReservedWords](http://wiki.rubyonrails.org/rails/pages/ReservedWords)

---

<div class="post-metadata">

**Author:** ![toddlc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e79b87/32.png) [@toddlc](https://www.ruby-forum.com/u/toddlc)\
**Post date:** [April 10, 2008, 1:45am UTC](https://www.ruby-forum.com/t/type-with-two-references-to-the-same-table/134333/4 "2008-04-10T01:45:58Z")

</div>

Yeah, type is used by Single Table Inheritance.

Julian

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![toddlc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e79b87/32.png) [@toddlc](https://www.ruby-forum.com/u/toddlc)\
**Post date:** [April 21, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/type-with-two-references-to-the-same-table/134333/5 "2008-04-21T15:35:22Z")

</div>

Thank you, thank you (both)! Changed my names to Entity and Attrib (I  
think attribute is also reserved). Never would have figured out  
the :foreign\_key business on my own.

Thanks again,

Todd
