# How to join on a field other than parent table's id?

**URL:** <https://www.ruby-forum.com/t/how-to-join-on-a-field-other-than-parent-tables-id/114459>\
**Category:** Rails\
**Created:** [September 12, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/how-to-join-on-a-field-other-than-parent-tables-id/114459 "2007-09-12T22:24:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [September 12, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/how-to-join-on-a-field-other-than-parent-tables-id/114459/1 "2007-09-12T22:24:21Z")

</div>

Hi,

I am putting xml into a database. I have one table, products, that  
has\_many product\_features.

I would like to use the product table’s id in the product\_features  
table, and join on that, but the problem is that the product table’s  
id does not exist until the product is in the database.

I would prefer to join on a different field – a string I make up  
randomly, and keep track of. How do I do that within my product and  
product\_feature models? I can’t find anything in the documentation  
that says anything about how to join on anything other than the parent  
table’s id.

I do see the :source method, but this appears to allow you to make an  
alias for a table name, but still forces you to use the parent table’s  
id.

Help appreciated.

Charlie

---

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [September 12, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/how-to-join-on-a-field-other-than-parent-tables-id/114459/2 "2007-09-12T22:52:27Z")

</div>

when I first started using rails active\_records I struggled because I  
had previously used a java O/R mapping tool that used a guid generator  
to build primary keys as soon as you instantiated a model object. This  
really simplifies dealing with foreign key relationships in complex  
models. But I think you can get used to living with the way rails  
deals with this. I use foreign key constraints in the database, which  
it seems many rails developers do not (?). I did some testing early  
on to figure out how rails works and these are the notes I kept on  
this:

-add child to parent and save the parent =\> saves both  
-add child to parent and save the child =\> error, null value in fk  
-set childs parent and save child =\> saves both  
-set childs parent and save parent =\> only saves parent

these seem like fairly reasonable rules to live by and I’ve gotten  
used to it. Are you having problems along these lines? Do you use fk  
constraints?

I would really recommend not trying to add a different key value if  
possible. I suspect there are other ways to solve your problem.

(warning: i’m no rails guru!)
