# Using two foreign keys to the same table

**URL:** <https://www.ruby-forum.com/t/using-two-foreign-keys-to-the-same-table/57014>\
**Category:** Rails\
**Created:** [April 6, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/using-two-foreign-keys-to-the-same-table/57014 "2006-04-06T15:41:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 6, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/using-two-foreign-keys-to-the-same-table/57014/1 "2006-04-06T15:41:12Z")

</div>

i am working on a task manager with ruby on rails. it is my first major  
project, so i’m still gettng my feet wet as i learn.

i’ve been trying to use the built in relationships to link my tables  
together, but i’m not able to use the same naming conventions. for each  
task, it has a field for the creator, and another field for the  
assignee. both of these fields reference the users table. obviously i  
can’t have two fields that say user\_id in my tasks table, so how do i  
get them to link right and specify a different field?

here is my current database schema:  
_Tasks_  
id int not null auto\_increment,  
task\_name varchar(100) not null,  
description text not null,  
user\_id int not null,  
assignee int not null,  
date\_due date not null,  
completed\_at datetime not null,  
constraint fk\_task\_user foreign key (user\_id) references users(id),  
constraint fk\_task\_assignee foreign key (assignee) references  
users(id),

_Users_  
id int not null auto\_increment,  
username varchar(20) not null,  
password varchar(20) not null,  
full\_name varchar(100) not null,  
email varchar(150) not null,  
primary key (id)

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 6, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/using-two-foreign-keys-to-the-same-table/57014/2 "2006-04-06T16:32:12Z")

</div>

Josh K. wrote:

> here is my current database schema:  
> users(id),
> 
> _Users_  
> id int not null auto\_increment,  
> username varchar(20) not null,  
> password varchar(20) not null,  
> full\_name varchar(100) not null,  
> email varchar(150) not null,  
> primary key (id)

(This is my first reply through the Gmane gateway, let’s hope it  
works…)

You don’t have to name your foreign key user\_id. Rails can indeed guess  
the  
correct relations when you do, but you can override. From the api docs:

belongs\_to :author, :class\_name =\> “Person”, :foreign\_key =\> “author\_id”

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000529](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000529)

You could do something like

belongs\_to :user, :foreign\_key =\> “creator\_id”  
belongs\_to :user, :foreign\_key =\> “assignee\_id”

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 6, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/using-two-foreign-keys-to-the-same-table/57014/3 "2006-04-06T16:37:24Z")

</div>

> [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000529)
> 
> You could do something like
> 
> belongs\_to :user, :foreign\_key =\> “creator\_id”  
> belongs\_to :user, :foreign\_key =\> “assignee\_id”

exactly what i was looking for. thanks.

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 6, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/using-two-foreign-keys-to-the-same-table/57014/4 "2006-04-06T20:17:29Z")

</div>

Jeremy E. wrote:

> I doubt that will work. Try:
> 
> belongs\_to :creator, :class\_name=\>‘User’, :foreign\_key =\> “creator\_id”  
> belongs\_to :assignee, :class\_name=\>‘User’, :foreign\_key =\> “assignee\_id”
> 
> Which is also untested, but more likely to work.

Oh yeah, whoops. The association needs a name 🙂

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 6, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/using-two-foreign-keys-to-the-same-table/57014/5 "2006-04-06T20:05:08Z")

</div>

On 4/6/06, Wiebe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You could do something like
> 
> belongs\_to :user, :foreign\_key =\> “creator\_id”  
> belongs\_to :user, :foreign\_key =\> “assignee\_id”

I doubt that will work. Try:

belongs\_to :creator, :class\_name=\>‘User’, :foreign\_key =\> “creator\_id”  
belongs\_to :assignee, :class\_name=\>‘User’, :foreign\_key =\> “assignee\_id”

Which is also untested, but more likely to work.
