总结多个记录

时间:2017-01-30 16:44:43

标签: ruby-on-rails model

我想为竞赛者提交的提交内容构建一个评分应用。将有几名评委可以为参赛者输入得分。这是分数模型:

class Score < ActiveRecord::Base
  validates :submission_id, presence: true
  validates :score1, presence: true
  validates :score2, presence: true
  validates :score3, presence: true
end

分数将显示在分数摘要页面中:

<div class="row">
  <div class="col-md-2">
    <p><strong>Score 1:</strong></p>
  </div>
 <% @submission.scores.each do |score| %>
  <div class="col-md-1">
    <%= score.score1 %>
  </div>
 <% end %>
</div>

<div class="row">
  <div class="col-md-2">
    <p><strong>Score 2:</strong></p>
  </div>
 <% @submission.scores.each do |score| %>
  <div class="col-md-1">
    <%= score.score2 %>
  </div>
 <% end %>
</div>

<div class="row">
  <div class="col-md-2">
    <p><strong>Score 3:</strong></p>
  </div>
 <% @submission.scores.each do |score| %>
  <div class="col-md-1">
    <%= score.score3 %>
  </div>
 <% end %>
</div>

<div class="row">
  <div class="col-md-2">
    <p><strong>Total Score:</strong></p>
  </div>
 <% @submission.scores.each do |score| %>
  <div class="col-md-1">
    <strong><%= ((0.25 * score.score1) + (0.25 * score.score2) + (0.50 * score.score3)) %></strong>
  </div>
 <% end %>
</div>

<div class="row">
  <div class="col-md-2">
    <p><strong>FINAL SCORE:</strong></p>
  </div>
  <div class="col-md-1">
    <strong><!-- Sum all of the total score above --></strong>
  </div>
</div>

该页面将显示每位评委提交的所有分数,并将最终分数中的每个评分进行求和。

现在,我想将每个评委的所有最终得分加总到最终得分总分中。如何实现?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

我看到你的分数也有乘法因此你想在那里使用它也是这样:

@submission.scores.sum('0.25 * (score1 + score2) + 0.5 * score3')

如果不是,请使用此:

@submission.scores.sum('score1 + score2 + score3')