# Has\_one association against a non primary key

**URL:** <https://www.ruby-forum.com/t/has-one-association-against-a-non-primary-key/128355>\
**Category:** Rails\
**Created:** [February 7, 2008, 12:42am UTC](https://www.ruby-forum.com/t/has-one-association-against-a-non-primary-key/128355 "2008-02-07T00:42:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [February 7, 2008, 12:42am UTC](https://www.ruby-forum.com/t/has-one-association-against-a-non-primary-key/128355/1 "2008-02-07T00:42:39Z")

</div>

Hey list,

Any easy way to do a has\_one, belongs\_to based off a key that is NOT  
the ID field?

I am working with a legacy table that has a grouping column in the  
table, I need to have a has\_one association off this grouping column.

Otherwise, I guess I’ll just have to make a method, which isn’t a big  
deal.

Regards

Mikel

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [February 12, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/has-one-association-against-a-non-primary-key/128355/2 "2008-02-12T23:20:55Z")

</div>

On Feb 6, 2008 5:41 PM, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any easy way to do a has\_one, belongs\_to based off a key that is NOT  
> the ID field?
> 
> I am working with a legacy table that has a grouping column in the  
> table, I need to have a has\_one association off this grouping column.
> 
> Otherwise, I guess I’ll just have to make a method, which isn’t a big deal.

Both has\_one and belongs\_to accept a :foreign\_key parameter.

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M001104](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M001104)  
[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M001105](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M001105)

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [February 13, 2008, 1:08am UTC](https://www.ruby-forum.com/t/has-one-association-against-a-non-primary-key/128355/3 "2008-02-13T01:08:25Z")

</div>

On Feb 13, 2008 9:20 AM, Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Feb 6, 2008 5:41 PM, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Any easy way to do a has\_one, belongs\_to based off a key that is NOT  
> > the ID field?
> > 
> > I am working with a legacy table that has a grouping column in the  
> > table, I need to have a has\_one association off this grouping column.
> > 
> > Otherwise, I guess I’ll just have to make a method, which isn’t a big deal.
> 
> Both has\_one and belongs\_to accept a :foreign\_key parameter.

No, I know that, but the foreign\_key param expects the source record’s  
ID value to be in the ID column.

What I am talking about is a self referrential table where a person  
might have several records in the table, but these records are grouped  
by a grouping value, like an entity id column.

So each record has it’s own unique ID, but many records could share  
the grouping ID, no one record is the “parent”, as each record in the  
group belongs to each other.

But maybe I’ll check out the self-refferential stuff again, I might  
have missed something obvious.

Mikel
