I have the following database schema structure:
School
has_many :students
Students
has_many :books
Books
(has an attribute pages(integer))
The issue is I want a total of all the pages of a book a student has at the school level. But there is also a possibility that a student might not have any book.
I want to select the following rails query using ruby code and methods like joins and includes:
SELECT * FROM school INNER JOIN students ON students.school_id = schools.id LEFT OUTER JOIN books ON books.student_id = students.id
I have tried the following:
School.joins({:students => :books}).all
But this will not take into account those students who do not have any books. How to solve this?
Aucun commentaire:
Enregistrer un commentaire