Jake Boxer
2010-Jan-18 05:50 UTC
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Eddy Josafat
2010-Jan-18 11:13 UTC
Re: Specifying the foreign key in a has_many :through relationship
Have you tried this?: has_many :complete_projects, :through => :completed_assignments, :source => :project -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-18 14:37 UTC
Re: Specifying the foreign key in a has_many :through relat
Eddy Josafat wrote:> Have you tried this?: > > > has_many :complete_projects, > :through => :completed_assignments, > :source => :projectNo need for :through. Just use :foreign_key. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. --0015174c0d5c338e4f047d714cd2 Content-Type: text/plain; charset=ISO-8859-1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --0015174c0d5c338e4f047d714cd2--