# One model using multiple foreign keys?

**URL:** <https://www.ruby-forum.com/t/one-model-using-multiple-foreign-keys/74420>\
**Category:** Rails\
**Created:** [November 6, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/one-model-using-multiple-foreign-keys/74420 "2006-11-06T18:36:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![neco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@neco](https://www.ruby-forum.com/u/neco)\
**Post date:** [November 6, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/one-model-using-multiple-foreign-keys/74420/1 "2006-11-06T18:36:29Z")

</div>

I have a simple question, but wasn’t sure of the best way to do this. So  
any help is greatly appreciated.

Let’s say I have users model and a vacations model. A user can add a  
vacation to the system, therefore that vacation belongs to that user as  
to say “this user added this vacation”. This relationship is set up by  
adding a user\_id field in the vacations table. Now a vacation can be  
reserved by only one user. So I add a field called “reserved\_by\_user\_id”  
in the vacations table. I now have 2 relationships between the vacations  
and users model. What is the best way to do this? I’d like to be able to  
do vacation.reserved\_by\_user.username.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![neco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@neco](https://www.ruby-forum.com/u/neco)\
**Post date:** [November 6, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/one-model-using-multiple-foreign-keys/74420/2 "2006-11-06T19:25:11Z")

</div>

Neco Himm wrote:

> I have a simple question, but wasn’t sure of the best way to do this. So  
> any help is greatly appreciated.
> 
> Let’s say I have users model and a vacations model. A user can add a  
> vacation to the system, therefore that vacation belongs to that user as  
> to say “this user added this vacation”. This relationship is set up by  
> adding a user\_id field in the vacations table. Now a vacation can be  
> reserved by only one user. So I add a field called “reserved\_by\_user\_id”  
> in the vacations table. I now have 2 relationships between the vacations  
> and users model. What is the best way to do this? I’d like to be able to  
> do vacation.reserved\_by\_user.username.
> 
> Thanks for your help.

Hi Neco,

I’ve done a similar thing, the way I did it is to create a  
ReservedByUser model, and in reserved\_by\_user.rb have the following:

class ReservedByUser \< ActiveRecord::Base

set\_table\_name “users”

end

and have ‘belongs\_to :reserved\_by\_user’ in vacations.rb

This should work, but there may be a better way to do it.

Dave.

---

<div class="post-metadata">

**Author:** ![neco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@neco](https://www.ruby-forum.com/u/neco)\
**Post date:** [November 6, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/one-model-using-multiple-foreign-keys/74420/3 "2006-11-06T19:35:30Z")

</div>

Hi Neco,

Without knowing in more details about what you are trying to accomplish,  
I may suggest something that is totally wrong. However, here is my  
suggestion anyway:

To me, your vacation table sounds more/less like reservation table. If I  
understand correctly, user\_id is your employee who added this new  
reservation while reserved\_by\_user\_id is your actual customer.

if your vacation class has user (model) as part of its member, when you  
are retrieving data from vacation table, you can also include the  
username JOIN by id. Then you can store these value in vacation object.

Hope this helps,

Will T.

Neco Himm wrote:

> I have a simple question, but wasn’t sure of the best way to do this. So  
> any help is greatly appreciated.
> 
> Let’s say I have users model and a vacations model. A user can add a  
> vacation to the system, therefore that vacation belongs to that user as  
> to say “this user added this vacation”. This relationship is set up by  
> adding a user\_id field in the vacations table. Now a vacation can be  
> reserved by only one user. So I add a field called “reserved\_by\_user\_id”  
> in the vacations table. I now have 2 relationships between the vacations  
> and users model. What is the best way to do this? I’d like to be able to  
> do vacation.reserved\_by\_user.username.
> 
> Thanks for your help.
