在控制台中,此代码:
Patient.joins(:notes,:recordings).find(1)
退货
ActiveRecord::RecordNotFound: Couldn't find Patient with id=1
这很奇怪,因为在同一个控制台中Patient.find(1)
可以正常工作并且检索ID为1的患者的记录。
我的理解是我应该能够做到:
a = Patient.joins(:notes,:recordings).find(1)
a.notes
a.recordings
并且a.notes应该返回与ID为1且与a.recordings相同的患者相关的所有注释。很明显,我在这里遗漏了什么...任何想法?
这是因为RailsINNER JOIN
默认情况下会这样做。你的病人1
没有任何notes
或recordings
如果可以的话可以做一个左联接。
Patient.joins("LEFT JOIN notes on notes.patient_id = patients.id")
.joins("LEFT JOIN recordings on recordings.patient_id = patients.id")
.find(1)
或加载患者,然后加载关联
a = Patient.find(1)
a.notes
a.recordings
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句