# Has\_one mapping with arbitrary right and left hand side keys

**URL:** <https://www.ruby-forum.com/t/has-one-mapping-with-arbitrary-right-and-left-hand-side-keys/50650>\
**Category:** Rails\
**Created:** [December 19, 2005, 6:23pm UTC](https://www.ruby-forum.com/t/has-one-mapping-with-arbitrary-right-and-left-hand-side-keys/50650 "2005-12-19T18:23:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonlog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/db5fbb/32.png) [@jonlog](https://www.ruby-forum.com/u/jonlog)\
**Post date:** [December 19, 2005, 6:23pm UTC](https://www.ruby-forum.com/t/has-one-mapping-with-arbitrary-right-and-left-hand-side-keys/50650/1 "2005-12-19T18:23:24Z")

</div>

I have two tables:

table\_1:  
tab1\_id,  
other\_id

table\_2:  
id,  
tab2\_id,  
something\_else

I would like to specify a has\_one mapping from table\_1 to table\_2 but  
specifying that the join is based on (table\_2.tab2\_id =  
table\_1.other\_id).

I am half way there with:

class Table1 \< ActiveRecord::Base  
set\_table\_name “table\_1”  
set\_primary\_key “tab1\_id”  
has\_one :Table2, :class\_name =\> “Table2”, :foreign\_key =\> “tab2\_id”  
end

However, as you can see, this will join on table\_2.tab2\_id =  
table1\_1.tab1\_id and not on table\_1.other\_id.

Is there a notation that I am missing to express this, or is there  
another way of acheiving this. I cannot alter the DB schema.

Thanks in anticipation.
