How to display data from a linking table
Good Day I am hoping you can help me, i am really new to all of this.
I have two models:
Contractor
has_many :employees
Employee
belongs_to :contractor
I then created a migration table which stores an id
, employee_id
and contractor_id
.
This allows me to link the two tables if i am correct?
Now the contractor is logging into the system which is work 100%, i would like him to be able to create employees and be able to view onl开发者_StackOverflow社区y his employees (not all the employees in the database).
What would be the best solution to this?
Thanks in advance!
No, that is not correct. You have a one (Contractor) to many (Employee) relationship. So what you have instead is a column in your employees
table that is called contractor_id
. This way you can use
Contractor#employees
to find all of your Employees that belong to a certain Contractor (source)
Good luck.
精彩评论