这是我的关联
Instructor has many Instructor Students
InstructorStudent has many Student Contacts
"学生联系人"表有一个具有布尔类型的字段primary_contact。
我想指导没有任何学生联系人或所有学生的学生 学生联系人的primary_contact是错误的。
例如
讲师学生 1 有 3 个学生联系人和所有 3 个学生 联系人的主要联系人字段为 false。比我想要那张唱片
教师学生 2有 3 个学生联系人,其中 2 个学生 联系人的主要联系人字段为假,其他学生联系人的 主要联系人是真实的,而不是我不想要那个记录。
我做了一个查询,但我不知道如何写 having 子句。
Instructor.joins(:instructor_students => :student_contacts)
.where("admin_users.id = ? AND student_contacts.primary_contact =
?",7,false)
.select("student_contacts.instructor_student_id,count(student_contacts.primary_contact) as sc_count")
.group("student_contacts.instructor_student_id")
.having("count(student_contacts.primary_contact) = ?",Dynamic_value)
任何帮助将不胜感激。 提前谢谢你。
请尝试这个,正如您所说,您想要InstructorStudent
对象,因此您应该加入该表。
InstructorStudent.joins(:student_contacts).where("student_contacts.primary_contact =
?",false)
.select("student_contacts.instructor_student_id,count(student_contacts.primary_contact) as sc_count")
.group("student_contacts.instructor_student_id")
.having("count(student_contacts.primary_contact) = ?",Dynamic_value)