One model using multiple foreign keys?

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.

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs