Active record association in rails 4/5

Viewed 17

I have the following four models and I need help with how to associate the projects model to the timesheet and task model. The models I have are as follows:

User
Timesheet => has_many :tasks
Project => beloings_to :task
Project => has_many :tasks
Task => belongs_to :timesheet
Task=> belongs_to :project

When I execute the following association I get a response as shown below;

User.find(1).time_sheets.find(6).tasks

[ #<Task:0x00007fbdf5419780 id: 15, description: "Fixed the mat issue", task_date: "May 17, 2021", hours_worked: 3, project_id: 2, created_at: Mon, 17 May 2021 16:57:28 UTC +00:00, updated_at: Mon, 17 May 2021 16:57:28 UTC +00:00, time_sheet_id: 6>, #<Task:0x00007fbdf54194b0 id: 16, description: "Built IOS and Android module", task_date: "May 17, 2021", hours_worked: 3,
project_id: 5, created_at: Mon, 17 May 2021 23:41:00 UTC +00:00,
updated_at: Mon, 17 May 2021 23:41:00 UTC +00:00, time_sheet_id: 6>, ]

How do I associate the project model to the response and be able to be able to get the project_name in the result? Thanks for the help.

0 Answers
Related