Context:
I have a Company
model that has many projects
. Each project
has many tasks
. The company
has many employees
.
The Employee
model is associated with Company
and not with projects
or tasks
. The Task
model does have a employee_name
attribute, since it is one employee per task (but more task per employee). An employee will have only one task
per project
.
Schema:
My question:
I'm trying to create an array that for each employee
stores the projects
it has a task
in.
I currently have the following code working, but I am wondering if I could do this in a better way. Should I change my model associations, or is this the way to go?
@employee = Employee.find(1)
@employee_tasks = Task.where(employee: @employee.name)
@employee_project = {}
i = 0
@employee_tasks .each do |f|
i += 1
@employee_project[i] = Project.where(id: f.project_id)
end