Rails has_many through query depending on the through table attribute

左心房为你撑大大i 提交于 2019-12-03 11:55:20
Patient.includes(:physicians, :appointments).where('physicians.id = ? AND appointments.appointment_date = ?', <id or ids array>, 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!