I'm trying to grab a list of active tasks for a workflow tool I'm making, with the data structured like this:
- User
- has_many Projects
- has_many Subprojects
- has_many Tasks
- has_many TimeLogs
- has_many Tasks
- has_many Subprojects
- has_many Projects
Active tasks are defined as any task with a TimeLog that does not have a 'completed' timestamp.
I'm trying to make the main page display this full structure, but only show parent structures that have an active task at some level. Any Users/Projects/Subprojects that don't have an active task beneath them should not be returned by the query.
So far I've tried:
- A join on all four tables, which produces duplicate rows
- A WHERE EXISTS statement, which returns only the relevant users, but doesn't maintain the WHERE clause when I try to access its children
Is there a way to achieve this without manually culling the data in Ruby?
.uniqonto the end of your join query. ActiveRecord will then query without the duplicates.whereclause through the children seems messy, so I'm looking for a way to just grab it all at once, maybe convert the ActiveRecord query to a nested Hash?