Reputation: 302
I have an application which has projects
and users
. I don't care about tracking the many-to-many relationships between users
and projects
but I am interested in tracking the many-to-one relationship between projects
and a specific user
, which is the project manager. Ideally, I would like to create a column in the projects
table called projectmanager_id
and link it to a user
. However, rails convention dictates that I use user_id
instead. While this works, I feel the semantics aren't quite right. Is there a way to have both?
One way I thought of is to create a projectmanagers
table with user_id
and project_id
and have a has_many :projects, :through=> :projectmanagers
in the user
model, and has_one :user, :through => :projectmanagers
in the project
model. Is there a better way?
Upvotes: 1
Views: 601
Reputation: 3717
You could try this if you keep the db column as user_id: In your project.rb file:
belongs_to :projectmanager, :foreign_key => "user_id", :class_name => "User"
And in your user.rb file still have:
has_many :projects
Or if it's that you want your db column to be projectmanager_id
In your project.rb file:
belongs_to :projectmanager, :foreign_key => "projectmanager_id", :class_name => "User"
In your user.rb file:
has_many :projects, :foreign_key => "projectmanager_id", :class => "Project"
Upvotes: 1