如何查询活动记录?
how to query active record?
我想看看我的哪些学生有证书。在学生模型 has_many :certifications
中。当我执行 Student.where(company_id:79).count
或 Student.where(company_id:79).all
查询时,它 returns 学生人数(748)和包含所有学生的数组。但是,我只能咨询是否有证书,每个学生一张一张。当我做 Student.where(company_id:79).certifications
它 returns 一个错误:(undefined method "certifications" for #<Student::ActiveRecord_Relation:0x0000564640516fd0>)
当我做 x = Student.where(company_id:79).last
和
x.certifcations
那么是的 returns 学生证与否
由于学生和证书之间存在一对多关联,因此您可以这样做:
Student.where(company_id: 79).joins(:certificates).distinct
我想看看我的哪些学生有证书。在学生模型 has_many :certifications
中。当我执行 Student.where(company_id:79).count
或 Student.where(company_id:79).all
查询时,它 returns 学生人数(748)和包含所有学生的数组。但是,我只能咨询是否有证书,每个学生一张一张。当我做 Student.where(company_id:79).certifications
它 returns 一个错误:(undefined method "certifications" for #<Student::ActiveRecord_Relation:0x0000564640516fd0>)
当我做 x = Student.where(company_id:79).last
和
x.certifcations
那么是的 returns 学生证与否
由于学生和证书之间存在一对多关联,因此您可以这样做:
Student.where(company_id: 79).joins(:certificates).distinct