我有学生参加了“测试”,其中有 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”的列。我不知道如何使用这个属性来获得我想要的结果。