Specifying the foreign key in a has_many :through relationship

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
(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.

Have you tried this?:

has_many :complete_projects,
:through => :completed_assignments,
:source => :project

Eddy J. wrote:

Have you tried this?:

has_many :complete_projects,
:through => :completed_assignments,
:source => :project

No need for :through. Just use :foreign_key.

Marnen Laibow-Koser
[email protected]

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