# Convention for multiple id reference

**URL:** <https://www.ruby-forum.com/t/convention-for-multiple-id-reference/152138>\
**Category:** Rails\
**Created:** [October 30, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/convention-for-multiple-id-reference/152138 "2008-10-30T14:12:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [October 30, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/convention-for-multiple-id-reference/152138/1 "2008-10-30T14:12:32Z")

</div>

Hi,

Here’s the thing :  
I have a table with languages (name, label, id).

languages  
id  
name  
label

I’d like another table people with primary\_language and  
secondary\_langage  
that should refer to a language\_id.

people  
id  
firstname  
familyname  
primary\_language -\> language\_id  
secondary\_language -\> language\_id

Because I got 2 references, I don’t know how I can do. I think I have to  
explicitly say in the Person and Language model that primary\_language  
and secondary\_language is a language\_id. How can I do ?

Thanks

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [October 30, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/convention-for-multiple-id-reference/152138/2 "2008-10-30T14:16:26Z")

</div>

Here’s how the table would look:

people  
id  
firstname  
familyname  
primary\_language → primary\_language\_id  
secondary\_language → secondary\_language\_id

And the class:

class Person  
belongs\_to :primary\_language, :class\_name =\> ‘Language’  
belongs\_to :secondary\_language, :class\_name =\> ‘Language’  
end

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
João Pessoa, PB, +55 83 8867-7208

On Thu, Oct 30, 2008 at 10:12 AM, David N.

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [October 30, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/convention-for-multiple-id-reference/152138/3 "2008-10-30T14:27:38Z")

</div>

For assigning the language id you need to assign the language\_id before  
saving the record. this should be

record.primary\_language\_id = language.id  
record.secondary\_language\_id = language.id  
record.save!

In your person model you can say,  
belongs\_to :primary\_language, :class\_name =\> “Language”, :foreign\_key  
=\> “primary\_language\_id”  
for the relation ship.  
So in this case People.primary\_language will be the language record.  
As for the Rails coding use the column name as “primary\_language\_id”  
instead of “primary\_language”.

> people  
> id  
> firstname  
> familyname  
> primary\_language -\> language\_id  
> secondary\_language -\> language\_id
> 
> Because I got 2 references, I don’t know how I can do. I think I have to  
> explicitly say in the Person and Language model that primary\_language  
> and secondary\_language is a language\_id. How can I do ?
> 
> Thanks
