「テスト」を受けた学生は5つの質問があります。私がしたいことは、各テストの各質問について最大のスコアを示すことです。Rubyはテーブルの属性を持つcollect/mapを使用します
Test、Student、Questionはすべて別のテーブルです。私が持っている
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
それは私が各テストの各質問のための最大のスコアを印刷したい5に各「question_number」1の最大を返しません。質問表で
例)
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 'という名前の列があります。私はこの属性を使って私が望む結果を得る方法を知らない。
あなたが他のループをしたい私には思えるし、質問の数を出力するすべての質問のオブジェクトの上に横断し、スコア属性あなたが今説明する方法では、maxメソッドを全く使用したくはありません。 – Maran
私の答えをチェックしてください。あなたが完全に間違っている収集を持っていることにも注意してください、それは必須ではありません(googleで '機能的なルビー'を検索してください) – tokland