私には、5つの「質問」がある「テスト」を受けた学生がいます。私がやりたいのは、各テストの各質問の最大「スコア」を表示することです。
テスト、学生、質問はすべて別々のテーブルです。
class Test < ActiveRecord::Base
has_many :students
end
class Student < ActiveRecord::Base
belongs_to :test
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :students
end
私が持っているコード:
<% @tests.each do |test| %>
<% max_score = [] %>
<% test.students.collect {|s| s.questions.collect {|q| max_score << q.score}}%>
<tr>
<th><%= test.name %></th>
<th><%= max_score.max %></th
</tr>
<% end %>
ただし、これが示すのは、テスト全体の最大スコアです。
例)
Math - 95
History - 98
Physics - 100
'question_number' 1〜5ごとの最大値は返されません。各テストの各質問の最大スコアを出力したいと思います。
例)
Math - 1 - 90
Math - 2 - 100
Math - 3 - 88
Math - 4 - 79
Math - 5 - 98
History - 1 - 80
History - 2 - 95
..and so on...
質問テーブルには、「question_number」という名前の列があります。この属性を使用して希望の結果を得る方法がわかりません。