# Models relations definition

**URL:** <https://www.ruby-forum.com/t/models-relations-definition/164412>\
**Category:** Rails\
**Created:** [April 1, 2009, 6:32am UTC](https://www.ruby-forum.com/t/models-relations-definition/164412 "2009-04-01T06:32:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [April 1, 2009, 6:32am UTC](https://www.ruby-forum.com/t/models-relations-definition/164412/1 "2009-04-01T06:32:30Z")

</div>

Hi, got an issue here ^^ Ok so my problem is that I have a table named  
“users\_accounts” and another one named “accounts\_messages”. As you  
will guess, accounts\_messages is a list of messages that  
“users\_accounts” can interchange. Here comes my problem:  
for each message I need to know who is the autor and the destinator,  
so in “accounts\_messages” I have two parameters which are:  
autor\_user\_account\_id (refers to the autor account) and  
user\_account\_id (refers to the destinator account).

So in my AccountMessage ruby model class I want to refer two times to  
the users\_accounts table. How do I do that? More precisely my question  
is how do I do to keep the relation navigability for the two  
parameters? If I declare those two lines:  
class AccountMessage \< ActiveRecord::Base  
belongs\_to :autor\_user\_account  
belongs\_to :user\_account

```
...

```

end

obviously autor\_user\_account wont refer alone to the UserAccount  
class. So how can I declare those two parameters in order to use them  
that way for exemple:

message = AccountMessage.find(…)  
printf “autor name: #{message.autor\_user\_account.name}”  
printf “destinator name: #{message.user\_account.name}”

We supose here that the “users\_accounts” table has a name property of  
course.

Hope it’s understandable 🙂  
Olivier.

---

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [April 1, 2009, 7:26am UTC](https://www.ruby-forum.com/t/models-relations-definition/164412/2 "2009-04-01T07:26:20Z")

</div>

> the users\_accounts table. How do I do that? More precisely my question  
> is how do I do to keep the relation navigability for the two  
> parameters? If I declare those two lines:  
> class AccountMessage \< ActiveRecord::Base  
> belongs\_to :autor\_user\_account  
> belongs\_to :user\_account
> 
> …  
> end
> 
> >

Look into the :class\_name and :foreign\_key parameters that go along  
with belongs\_to/has\_one/has\_many.

:class\_name  
Specify the class name of the association. Use it only if that  
name can‘t be inferred from the association name. So has\_one :author  
will by default be linked to the Author class, but if the real class  
name is Person, you‘ll have to specify it with this option.

:foreign\_key  
Specify the foreign key used for the association. By default this  
is guessed to be the name of the association with an “\_id” suffix. So  
a class that defines a belongs\_to :person association will use  
“person\_id” as the default :foreign\_key. Similarly,  
belongs\_to :favorite\_person, :class\_name =\> “Person” will use a  
foreign key of “favorite\_person\_id”.

---

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [April 1, 2009, 10:11am UTC](https://www.ruby-forum.com/t/models-relations-definition/164412/3 "2009-04-01T10:11:44Z")

</div>

Great, thank you very mucho for the help! ^^
