To provide a simple example, let's say I want to load ALL hospitals for an organization and preload ONLY the doctors that have a calendar, but at the same time, I do not want to create any scopes or additional associations in the models, so the models should remain the same. How would I do that?
With some pseudo code, I want something along the lines of:
def find_facilities(organization) doctors_query = Doctor.joins(:calendar).includes(:calendar) organization.facilities.preload(doctors: doctors_query)end
It's also important that not only do I want to check that the calendar exists, I also want to preload it.
And another point is that the calendar has a foreign key to the doctors, not the other way around.