lundi 10 octobre 2016

How to group the records based to 3rd table belongs to, has many rails 4

I have below code:

quiz.rb

has_many :quiz_attempts

student.rb

has_many :quiz_attempts

quiz_attempt.rb

belongs_to :student
belongs_to :quiz

What I need to do is to get all records of student from quiz_attempts table which has quiz id and show the records of quiz table based on it.

Means, if students has taken one quiz A 10 times and quiz B 5 times, so I need to show all these rows as 2 from quiz table.

In view I will have only 2 records of quiz table which are associated with 15 records of A and B collectively in quiz_attempts table.

quiz_attempts has quiz_id column and student_id column as well, quiz table has no student id in it.

The query I am working with is:

@quizzes   = @student.quiz_attempts.includes(:quiz)

but the problem is that, it still shows me 2 rows but from quiz_attempts table and I cant group them as quiz table.

I need to have complete solution.

Aucun commentaire:

Enregistrer un commentaire