In the project management app I’m working on, I’m currently working on a page for managing tickets, which I want should contain of the following:
- The tickets that the user has created
- The tickets that belongs to projects that the user has created
The tricky part is to use the right code in the controller, which is where I need help.
‘@users_tickets’works fine, as well as ‘@owned_projects’. However, the last thing which is creating an array that contains of the tickets that belongs projects that the user owns, is something I need help me (yes, I understand that my poor try with an each loop is totally the wrong way to go here).
How can I achieve what I want?
Tickets controller:
1. def manage
2. @users_tickets = Ticket.where(:user_id => current_user.id)
3. @owned_projects = Project.where(:user_id => current_user)
4.
5. @owned_projects.each do |project|
6. @tickets_for_owned_projects = Ticket.where(:project_id => project.id)
7. end
8. end
Tables:
tickets table:
project_id
ticket_status_id
user_id
title
description
start_date
end_date
projects table:
user_id
title
description
start_date
end_date
If you’re using a
has_manyassociation, then it should as simple asUPD: The approach above should work. I’m literally falling asleep right now and can’t define what is wrong here. Would really appreciate if someone looked into it.
Here’s another way around though.