Jau L
Jau L

Reputation: 904

has_many through associations where and select results

So I'm trying to filter results using WHERE condition but it's not working.

class Physician < ApplicationRecord
  has_many :appointments
  has_many :patients, through: :appointments
 end

class Appointment < ApplicationRecord
  belongs_to :physician
  belongs_to :patient
 end

class Patient < ApplicationRecord
  has_many :appointments
  has_many :physicians, through: :appointments
end

and what I'm doing here is:

patients = Patient.joins(:physicians).includes(:physicians).where(:appointments => {:sick => true })

However, what I'm getting is the following:

"results": {
  "patient": {
    "id": 2,
    "patient_name": "Rob"
   },
   "physicians": [
    {
      "id": 4,
      "physicians_name":...
      "sick":false
    ... ,
    {
      "id": 7,
     "physicians_name":...
     "sick":true
     ... 
     ... ,
      {
      "id": 7,
      "physicians_name":...
      "sick":false
     ... 
     ]
    }

So as you noticed, I'm NOT getting only physicians with sick=true. any suggestions?

Update: This is my serializer

class PatientSerializer < ActiveModel::Serializer
  attributes :records

 def records
    { info: records_info }
 end

 def records_info
  object.appointments.map do |a|
  a.attributes.merge(a.physician.attributes)
 end

end end

Upvotes: 2

Views: 73

Answers (2)

Michael Gaskill
Michael Gaskill

Reputation: 8042

As the sick attribute is stored on the appointment, you should joins(:appointments), instead of joins(:physicians).

Given that, this should return the results that you're looking for:

patients = Patient.joins(:appointments).includes(:physicians).where(appointments: { sick: true })

The reason for this is that the conditions in the where clause need to reference the joined tables. Since the physicians table isn't used in the where clause, it's not needed to be explicitly joined; however, the appointments table does need to be explicitly joined.

The includes(:physicians) allows "eager loading" of the physicians records, which prevents this from becoming an N+1 query when accessing each physician's data in the serializer.

Upvotes: 1

Nader-a
Nader-a

Reputation: 123

I believe includes(:physicians) is causing an eager loading, which makes the where clause not work as expected. Can you take it out and see if it fixes it? you can stick it at the end and see if it helps too.

Upvotes: 1

Related Questions