# How to relate models on field other than id

**URL:** <https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840>\
**Category:** Rails\
**Created:** [March 13, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840 "2008-03-13T18:45:56Z")\
**Posts on this page:** 9\
**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:** [March 13, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840/1 "2008-03-13T18:45:56Z")

</div>

Hi,

The answer might be obvious, but I sure can’t find it.

How do I related two tables in a model using a parent (the one side of  
a one-to-many relationship) field other than “id”, and a child (the  
many side of a one-to-many relationship) field other than  
“model\_name\_id”?

Yes, I can write sql, and no, I don’t want to. Because it’s so much  
nicer to keep everything in the ROR framework. will\_paginate, for  
example.

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:** [March 13, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840/2 "2008-03-13T18:48:25Z")

</div>

with foreign\_key like

has\_many :clients, :foreign\_key =\> :another\_column\_name

---

<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:** [March 13, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840/3 "2008-03-13T18:49:31Z")

</div>

Can you express what you’re doing in SQL? I’m assuming both of these  
other fields are unique in both tables, because if not that’d be a  
problem. Is there a reason why you don’t want to make the  
relationships based on the ID of the records? Just curious.

Nicholas

---

<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:** [March 13, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840/4 "2008-03-13T18:52:32Z")

</div>

sure,

select \* from parents, children where parents.column\_to\_match =  
children.column\_to\_match;

---

<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:** [March 13, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840/5 "2008-03-13T19:00:11Z")

</div>

I tried that, and I cannot get it to work.

Here is the exact problem. I have two tables, apache\_logs and  
ip\_hostnames. Each has a field, “ip”, and I would like to relate the  
two tables based on ip. These tables were created in ROR, so that  
each has an autoincrement field as its id.

In sql, I just say:

select \* from apache\_logs, ip\_hostnames where apache\_logs.ip =  
ip\_hostnames.ip

How do I do this in ROR? like I say, it’s a lot better to have all  
this stuff happen in the ROR framework, for all the code I already  
have in place – rather than do a whole new style of pagination just  
for one set of model relationships / actions / views.

Charlie

On Mar 13, 10:48 am, Thorsten M. \<rails-mailing-l…@andreas-

---

<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:** [March 13, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840/6 "2008-03-13T19:04:18Z")

</div>

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)  
[http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000752](http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000752)

You can use foreign\_key, see above link for syntax and examples.

:foreign\_key - specify the foreign key used for the association. By  
default this is guessed to be the name of this class in lower-case and  
\_id suffixed. So a Person class that makes a has\_one association will  
use person\_id as the default foreign\_key.

Example:

```
 belongs_to :firm, :foreign_key => "client_of"

```

I hope that helps.

Nicholas

---

<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:** [March 13, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840/7 "2008-03-13T19:15:38Z")

</div>

Ok, so if I do this in the console:

logs = apache\_logs.find(:all, :include =\> ip\_hostname, :limit =\> 10)

then

for l in logs  
puts l.ip\_hostname.inspect  
end

i get

nil  
nil  
nil  
nil  
nil  
nil  
nil  
nil  
nil  
nil

What am I doing wrong?

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:** [March 13, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840/8 "2008-03-13T19:24:31Z")

</div>

Ok, from your example:

so the related column in the child table is client\_of, right?

Is the related column in the parent table, in your example, still the  
id column?

so that the firm table is, let’s say:

id  
firm\_to  
…

the person table is

id  
client\_of

and the two tables are related by firm.id -\> person.client\_of?

What if I don’t want to use firm.id, but instead firm.firm\_to?

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:** [March 13, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/how-to-relate-models-on-field-other-than-id/131840/9 "2008-03-13T19:01:43Z")

</div>

charlie caroff wrote:

> How do I related two tables in a model using a parent (the one side of  
> a one-to-many relationship) field other than “id”, and a child (the  
> many side of a one-to-many relationship) field other than  
> “model\_name\_id”?

If I understand the question correctly, you can use the foreign\_key  
spec. I assume you’d just add the foreign\_key spec to your “belongs\_to”

class Child \< ActiveRecord::Base  
belongs\_to :parent\_model, :foreign\_key =\> ‘parent\_key\_field’  
end
