Rails has_many: through another column name in the join table

I currently have two different models: User and Project . The User model has three types of users — owners, contractors, and customers. I want to assign several contractors to one project. I am trying to do this with the has_many :through association, for example:

 Class User has_many :assignments has_many :projects, :through => :assignments Class Project has_many :assignments has_many :contractors, :through => :assignments Class Assignment belongs_to :user belongs_to :project 

My problem is using contractor_id in the assignments table instead of user_id .

In my assignments table, I currently have the columns contractor_id and project_id . Everything seems to work if I use user_id instead, but this will cause the situation to be pretty dirty in my views.

How to do it?

+8
ruby-on-rails ruby-on-rails-3
source share
1 answer

You should use the :foreign_key option in Assignment , for example:

 class Assignment belongs_to :user, :foreign_key => :contractor_id belongs_to :project 
+14
source share

All Articles