# Specifying the foreign key in a has\_many :through relationship

**URL:** <https://www.ruby-forum.com/t/specifying-the-foreign-key-in-a-has-many-through-relationship/181779>\
**Category:** Rails\
**Created:** [January 18, 2010, 6:52am UTC](https://www.ruby-forum.com/t/specifying-the-foreign-key-in-a-has-many-through-relationship/181779 "2010-01-18T06:52:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/65b543/32.png) [@mark42](https://www.ruby-forum.com/u/mark42)\
**Post date:** [January 18, 2010, 6:52am UTC](https://www.ruby-forum.com/t/specifying-the-foreign-key-in-a-has-many-through-relationship/181779/1 "2010-01-18T06:52:09Z")

</div>

I have the following three models: User, Project, and Assignment.

A User has\_many Projects through an assignment. However, Assignment  
actually has TWO foreign keys that relate to a user: user\_id  
(representing the user who was assigned the project) and completer\_id  
(representing the user who completed the project).

Often, user\_id and completer\_id will be the same (if the user who was  
assigned the project completes it). However, if another user completes  
it, user\_id and completer\_id will be different.

In my User model, I have the following: [http://pastebin.com/m454a1bfa](http://pastebin.com/m454a1bfa)  
(see the highlighted :incomplete\_projects association for the  
important stuff)

I would like to make another association, called :completed\_projects,  
which uses completer\_id as the foreign key in the :through model,  
rather than :user\_id. Is it possible to do this?

And, as an aside, I am aware of the :foreign\_key option. However, this  
option is ignored when using :through, so I’d like to know if there’s  
a way to do this without it. Also, I should mention that I’m open to  
other designs, if it can’t be done this way and someone can think of a  
better way.

---

<div class="post-metadata">

**Author:** ![mark42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/65b543/32.png) [@mark42](https://www.ruby-forum.com/u/mark42)\
**Post date:** [January 18, 2010, 12:15pm UTC](https://www.ruby-forum.com/t/specifying-the-foreign-key-in-a-has-many-through-relationship/181779/2 "2010-01-18T12:15:13Z")

</div>

Have you tried this?:

has\_many :complete\_projects,  
:through =\> :completed\_assignments,  
:source =\> :project

---

<div class="post-metadata">

**Author:** ![mark42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/65b543/32.png) [@mark42](https://www.ruby-forum.com/u/mark42)\
**Post date:** [January 18, 2010, 3:37pm UTC](https://www.ruby-forum.com/t/specifying-the-foreign-key-in-a-has-many-through-relationship/181779/3 "2010-01-18T15:37:49Z")

</div>

Eddy J. wrote:

> Have you tried this?:
> 
> has\_many :complete\_projects,  
> :through =\> :completed\_assignments,  
> :source =\> :project

No need for :through. Just use :foreign\_key.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
