I've looked into this SO question but I still have trouble wrapping my head around the concept.
I have a similar setup with the linked SO question, in that I have a User
class that contains both Employees and Managers. I also have another model for Role
(holding the role names) and UserRole
(holding which user has which role).
My requirements state that an Employee (a User
whose Role
is User
) can only have one Manager (a User
whose Role
is Manager
). Now, this Managering concept is an addition to the current system, and I'm not supposed to change the users
table, so I'm making a new table with their own MVC.
But now I find it hard to use has_many
and belongs_to
like the linked question. How do I use the new model? I tried using :through
but it doesn't work (for some reason).
Am I doing it wrong? Should I just add a manager_id
column to users
and work the solution in the linked question into my problem? Also, how do I ensure that only a User
whose Role
is Manager
can be set as a Manager?
Note: I have to say that I'm relatively new to Rails and ActiveRecord, and even Ruby in general.
Note 2: I'm using Rails 4.2.0 if it's relevant.