# Belongs\_to with foreign keys that reference non primary key

**URL:** <https://www.ruby-forum.com/t/belongs-to-with-foreign-keys-that-reference-non-primary-key/113435>\
**Category:** Rails\
**Created:** [August 31, 2007, 1:35am UTC](https://www.ruby-forum.com/t/belongs-to-with-foreign-keys-that-reference-non-primary-key/113435 "2007-08-31T01:35:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [August 31, 2007, 1:35am UTC](https://www.ruby-forum.com/t/belongs-to-with-foreign-keys-that-reference-non-primary-key/113435/1 "2007-08-31T01:35:08Z")

</div>

Hello,  
I have a situation where the foreign key into a table doesn’t  
correspond to that table’s primary key:

## my\_table

id (pk)  
name  
…

## other\_table

id (pk)  
my\_table\_name (fk references my\_table(name))  
…

I want to be able to say something like:  
class OtherTable \< ActiveRecord::Base  
belongs\_to :my\_table, :foreign\_key =\> { :my\_table\_name =\> :name }  
end

OtherTable.find(:first).my\_table

Is this possible? If not, what is the reasoning behind the decision?

Thanks.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [September 12, 2007, 8:33pm UTC](https://www.ruby-forum.com/t/belongs-to-with-foreign-keys-that-reference-non-primary-key/113435/2 "2007-09-12T20:33:57Z")

</div>

On Aug 30, 6:34 pm, “Christopher J. Bottaro” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> other\_table  
> OtherTable.find(:first).my\_table
> 
> Is this possible? If not, what is the reasoning behind the decision?
> 
> Thanks.

Is this possible?
