【发布时间】:2021-10-30 12:11:15
【问题描述】:
我想看看我的哪些学生有证书。在学生模型 has_many :certifications 中。当我执行Student.where(company_id:79).count 或Student.where(company_id:79).all 查询时,它会返回学生人数(748 人)和包含所有学生的数组。但是,我只能咨询是否有证书,每个学生一份一份。当我执行Student.where(company_id:79).certifications 时,它会返回一个错误:(undefined method "certifications" for #<Student::ActiveRecord_Relation:0x0000564640516fd0>)
当我做x = Student.where(company_id:79).last 和
x.certifcations 那么是的,它是否返回学生的证书。
【问题讨论】:
-
你必须使用joins。
Student.where(company_id:79).joins(:certifications)
标签: sql ruby-on-rails ruby activerecord orm