jamal
jamal

Reputation: 75

how to query active record?

I want to see which of my students have certificates. In the Student model has_many :certifications . When I do the Student.where(company_id:79).count or the Student.where(company_id:79).all query it returns both the number of students (which are 748) and an array with all students. However, I can only consult if there are certificates, one by one per Student. When I do Student.where(company_id:79).certifications it returns an error: (undefined method "certifications" for #<Student::ActiveRecord_Relation:0x0000564640516fd0>)

And when I do x = Student.where(company_id:79).last and x.certifcations then yes it returns the student's certificate or not.

Upvotes: 0

Views: 54

Answers (1)

Vibol
Vibol

Reputation: 1168

Since you have one to many association between student and certificate, you can do it this way: Student.where(company_id: 79).joins(:certificates).distinct

Upvotes: 1

Related Questions