Rails has_many through query depending on the through table attribute

前端 未结 1 444
我在风中等你
我在风中等你 2021-02-07 20:37

Having some problems with a has_many through query.

Using the example here: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

<
1条回答
  •  忘了有多久
    2021-02-07 21:07

    Patient.includes(:physicians, :appointments).where('physicians.id = ? AND appointments.appointment_date = ?', , Date.today)
    

    Where Date.today could be changed with anything and the pysician is specified by an id or an array of ids.

    You could also do:

    physician = Physician.find(id)
    patients = physician.patients.includes(:appointments).where('appointments.appointment_date  = ?', some_date)
    

    Edit:

    In Rails 4 and forward, you need to add references(:appointments) to the query in order to use appointments in the where clause.

    0 讨论(0)
提交回复
热议问题