Given the following models, how can I eager load a doctors specialty so I don't get crushed in a loop? Right now I'm able to load the Doctor
and User
user models but I'd also like to be able to load their profiles
and, if possible, the doctors specialty.
MedicalRelationship.includes(:doctor, :user).where(user_id: [1,2,3])
class MedicalRelationship < ActiveRecord::Base
belongs_to :user
belongs_to :doctor, :class_name => "User"
end
class DoctorProfile < ActiveRecord::Base
has_one :user, as: :profile, dependent: :destroy
belongs_to :specialty
end
class PatientProfile < ActiveRecord::Base
has_one :user, as: :profile, dependent: :destroy
end
class Specialty < ActiveRecord::Base
has_many :doctors, class_name: "DoctorProfile"
end