In Rails, how do I create a scope that filters on a related has_one attribute? I have two models, Patient
and Appointment
. Patient
declares a has_many
relationship on Appointment
s. Now I am adding a next_appointment
relationship:
class Appointment < ActiveRecord::Base
end
class Patient < ActiveRecord::Base
has_many :appointments, class_name: "::Appointment", foreign_key: :patient_id, inverse_of: :patient
has_one(
:next_appointment,
-> { where("appointment_date >= now()").order(:appointment_date).limit(1) },
class_name: "::Appointment",
foreign_key: :patient_id
)
scope :by_range_next_appointment_date, lambda { |from, to|
where(...)
}
end
Now I want to create a scope that returns all patients who have their next appointment within a given range. How can I fill in the where()
to accomplish this?
Here's an example to illustrate:
Let's say Bruce Banner has an appointment on 11/2/2021 and Peter Parker has appointments on 10/27/2021 and 11/3/2021. Now I want this scope to return all patients who's next appointment (as of 10/26/2021) which is between 11/1/2021 and 11/7/201. This should only return Bruce Banner since his next appointment is in that range. Peter Parker's next appointment is tomorrow, so he shouldn't be included.