I have been working on an scheduling app, and running into an issue with finding a way to efficiently get specific records.
Here are the models in question.
class Task < ApplicationRecord
belongs_to :user, optional: true
belongs_to :project, optional: true
belongs_to :project_schedule, optional: true
belongs_to :user_schedule, optional: true
class User < ApplicationRecord
include Filterable
validates :email, presence: true
validates :email, uniqueness: true
has_many :tasks
What the front end expects is a json of all users, with all tasks nested within them. This has become too cumbersome to load that many tasks and I was attempting to specify a date range to the tasks while still getting all users.
So this seems more difficult that I originally expected. I can get all users efficiently, all tasks within a date range, and all users with tasks within a given date range with the tasks included. But getting all users with the tasks nested within a given date range has evaded me.
What I've tried
User.filter(params.slice(:project_ids, :email, :id))
.includes(:tasks).where('((tasks.end_date BETWEEN ? AND ?)
OR (tasks.start_date BETWEEN ? AND ?))',
week_start, week_end, week_start, week_end)
.references(:tasks).order(:name)
I've also tried merging all users and all users with tasks within the range to no avail.
I feel like key is raw SQL but I couldn't wrap my head around the query yesterday.
Any insight would be helpful and appreciated.