3

我在部分视图中有以下代码,以显示每个评估期的学生平均分数:

<tr class="<%= cycle("odd", "even", name: "students")%>">
    <td>
    <%= link_to "#{student.name}", 
                 student_path({student_group_id: student.student_group_id, id: student.id})%>
    </td>
    <% student.eval_count.times do |i| %>
      <td class="center"><%= student.avg_for_eval(i) %></td>
    <% end %>
    <td class="center"><%= student.avg unless student.avg.nan? %></td>
</tr>

我正在使用这种方法student.rb来生成平均分数,起初我无法弄清楚为什么在没有数据的情况下我无法让它生成空行。

def evals
  evals = self.evaluations.order("eval_number").group_by(&:eval_number)
end

def eval_number_set(index)
  numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
  numbers[index]
end

def avg_for_eval(i)
  scores = []
  evals = self.evals.select { |k, v| k == self.eval_number_set(i) }.values.first
  for eval in evals
    scores << eval.score
  end
  evals.empty? #(scores.sum.to_f / scores.size).round(2)  
end

我将方法的最后一行更改为evals.empty?如上所示,并在浏览器中得到了这个:

浏览器

然后我意识到,由于select任何评估,学生不在场(在新学生的情况下)不是我用来生成代码的数据集的一部分。

正如您在图片中看到的那样,问题在于只有最后一次评估的数据的两个学生没有将他们的数据放在正确的列中 - 一切都向左移动,因为<td>视图代码没有生成.

那么问题是如何重写方法代码以获得相同的输出,但if scores.empty?方法插入""or"no data"或其他占位符以便<td>在视图中打印 a ?

更新

我现在更好地了解发生了什么。这种方法:

def eval_number_set(index)
  numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
  numbers[index]
end

正在返回每个学生的评估数字 - 所以当这些数字用于下一个方法时

def avg_for_eval(i)
  scores = []
  evals = self.evals.select { |k, v| k == self.eval_number_set(i) }.values.first
  for eval in evals
    scores << eval.score
  end
  evals.empty? #(scores.sum.to_f / scores.size).round(2)  
end

它只能提取学生已经出席的评估。现在回到绘图板......

更新 2

我已将类方法更改如下:

#returns all 'eval_number's for a given group of students 
#as the first student will have been present for all evaluations

def eval_number_set(index)
  numbers = self.student_group.students.first.evals.keys
  # numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
  numbers[index]
end

#attempts to match the first present 'eval_number' for a given student against
#the first number in the set of all 'eval_number's and react accordingly
def avg_for_eval(i)
  scores = []
  if self.evals.keys[i] == self.eval_number_set(i)
    "match"
  else
    "no_match"
  end  
end

这匹配了参加每次评估的所有学生,但不匹配错过一些学生的任何评估。我将代码更改为以下

  def avg_for_eval(i)
    scores = []
    if self.evals.keys[i] == self.eval_number_set(i)
      "#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
    else
      "#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
    end          
  end

它在浏览器中返回以下内容:

新问题

所以我尝试在 if 语句中添加一个计数器,这样如果语句匹配,它将增加正在尝试的键,否则它将保持在同一个键上:

  def avg_for_eval(i)
    scores = []
    key_match = 0
    if self.evals.keys[key_match] == self.eval_number_set(i)
      "#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
      key_match += 1
    else
      "#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
    end  
  end

产生这个:

在此处输入图像描述

并且是有道理的 - 由于调用方式,计数递增不会产生任何实际影响。但是虽然我认为我现在更接近我想要的效果,但我不知道如何实现它!

更新 3

更近了......我已经改变了一些事情,以便反向读取键,所以正在读取所有数据- 但我仍然希望在右侧打印最新的数据(见下图)。模型方法代码现在如下所示:

  def eval_number_set(index)
    numbers = self.student_group.students.first.evals.keys.reverse
    # numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
    numbers[index]
  end
  
  def avg_for_eval(i)
    scores = []
    eval_number = self.eval_number_set(i)
    if self.evals.keys.reverse[i] == eval_number
      for eval in self.evals.values[i] 
        scores << eval.score if self.evals.values[i]
      end
      scores
    else
      "no data"
    end  
  end

它返回以下内容,我已对其进行了注释,因此您可以更清楚地了解我正在寻找的内容:

在此处输入图像描述

更新 4

调用student.evals返回以下内容(对于id32 岁的学生):

{29=>[
  #<Evaluation id: 1949, score: 3, created_at: "2013-08-28 09:44:32", updated_at: "2013-08-28 09:44:32", student_id: 32, goal_id: 63, eval_number: 29>, 
  #<Evaluation id: 1950, score: 4, created_at: "2013-08-28 09:44:32", updated_at: "2013-08-28 09:44:32", student_id: 32, goal_id: 64, eval_number: 29>, 
  #<Evaluation id: 1951, score: 5, created_at: "2013-08-28 09:44:32", updated_at: "2013-08-28 09:44:32", student_id: 32, goal_id: 65, eval_number: 29>], 
30=>[
  #<Evaluation id: 1957, score: 3, created_at: "2013-08-28 09:44:43", updated_at: "2013-08-28 09:44:43", student_id: 32, goal_id: 65, eval_number: 30>, 
  #<Evaluation id: 1956, score: 2, created_at: "2013-08-28 09:44:43", updated_at: "2013-08-28 09:44:43", student_id: 32, goal_id: 64, eval_number: 30>, 
  #<Evaluation id: 1955, score: 1, created_at: "2013-08-28 09:44:43", updated_at: "2013-08-28 09:44:43", student_id: 32, goal_id: 63, eval_number: 30>], 
31=>[
  #<Evaluation id: 1968, score: 2, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 70, eval_number: 31>, 
  #<Evaluation id: 1967, score: 2, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 69, eval_number: 31>, 
  #<Evaluation id: 1966, score: 1, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 68, eval_number: 31>, 
  #<Evaluation id: 1965, score: 1, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 67, eval_number: 31>, 
  #<Evaluation id: 1964, score: 1, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 66, eval_number: 31>, 
  #<Evaluation id: 1963, score: 3, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 65, eval_number: 31>, 
  #<Evaluation id: 1962, score: 3, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 64, eval_number: 31>, 
  #<Evaluation id: 1961, score: 3, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 63, eval_number: 31>], 
32=>[
  #<Evaluation id: 1983, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 69, eval_number: 32>, 
  #<Evaluation id: 1982, score: 2, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 68, eval_number: 32>, 
  #<Evaluation id: 1981, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 67, eval_number: 32>, 
  #<Evaluation id: 1980, score: 4, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 66, eval_number: 32>, 
  #<Evaluation id: 1979, score: 4, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 65, eval_number: 32>, 
  #<Evaluation id: 1978, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 64, eval_number: 32>, 
  #<Evaluation id: 1977, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 63, eval_number: 32>, 
  #<Evaluation id: 1984, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 70, eval_number: 32>]
}
4

5 回答 5

3

更新

您应该更改eval_count方法或使用常量值来制作 4 个单元格。例如,

4.times do |i|
  <td class="center"><%= student.avg_for_eval(i) %></td>
end

更新结束

您可以尝试这种方法:

def avg_for_eval(i)
  scores = Array.new(4)
  evals = self.evals.select { |k, v| k == self.eval_number_set(i) }.values.first
  evals.each_with_index do |eval, i|
    scores[i] = eval.score
  end
  (scores.sum.to_f / scores.size).round(2)  
end

注意分数数组初始化。在这个方法中 [nil,nil,nil,nil] 最初被创建。对于只有两个分数的学生,分数将等于 [first_score, second_score, nil, nil]。

它导致可能的错误:方法的结果是

(first_score + second_score) / 4
于 2013-08-30T12:01:51.817 回答
0

要打印一个空的表格单元格,请在单元格内放置一个不间断的空格。

 <td>&nbsp;</td>
于 2013-09-06T23:36:15.857 回答
0

上次更新后,修改视图如下:

<tr class="<%= cycle("odd", "even", name: "students")%>">
<td>
<%= link_to "#{student.name}", 
             student_path({student_group_id: student.student_group_id, id: student.id})%>
</td>
<% student.eval_count.times do |i| %>
  <td class="center"><%= student.avg_for_eval(student.eval_count - i) %></td>
<% end %>
<td class="center"><%= student.avg unless student.avg.nan? %></td>
</tr>

基本上,以相反的顺序显示它们

于 2013-09-02T16:02:44.173 回答
0

我终于用下面的代码完成了这个工作 - 但如果有人能告诉我一个更好的方法来做到这一点,我很高兴给予他们赏金。

_student_list.html.erb

<table class="fixed">
  <tbody>      
    <% group.students.each_with_index do |student, index| %>
    <tr>
      <th></th>
      <% if index == 0 %>
        <% student.eval_count.times do |i| %>                                             
          <th class="center">Evaluation <%= i + 1 %></th>
        <% end %>
        <th><%= "Student average" if student.eval_count > 0  %></th>
      <% end %>

    </tr>
    <tr class="<%= cycle("odd", "even", name: "students")%>">
      <td>
        <%= link_to student.name, student_path({student_group_id: student.student_group_id, id: student.id})%>
      </td>
      <% student.student_group.eval_count.times do |i| %>
        <td class="center"><%= student.avg_for_eval(i) %></td>
      <% end %>
      <td class="center"><%= student.avg unless student.avg.nan? %></td>
    </tr>
    <% end %>
    <% reset_cycle("students") %>
  </tbody>
</table>

student.rb

  def eval_number_set(index)
    numbers = self.student_group.students.first.evals.keys
    numbers[index]
  end

  def reverse_eval_number_set(index)
    numbers = self.student_group.students.first.evals.keys.reverse
    numbers[index]
  end

  def avg_for_eval(i)
    scores = []
    eval_number = self.reverse_eval_number_set(i)
    count_differential = (self.student_group.eval_count - self.eval_count) - i
    if self.student_group.eval_count == self.eval_count
      for eval in self.evals.values[i] 
        scores << eval.score
      end
      (scores.sum.to_f / scores.size).round(2)
    else
      if self.evals.values[count_differential]
        for eval in self.evals.values[count_differential]
          scores << eval.score
        end
        (scores.sum.to_f / scores.size).round(2)
      else
        "no data"
      end  
    end  
  end
于 2013-09-05T15:30:59.927 回答
0

假设group.eval_count是最大数量eval_count

看法

<table class="fixed">
  <thead>      
    <tr>
      <th></th>
      <% group.eval_count.times do |i| %>                                             
      <th class="center">Evaluation <%= i + 1 %></th>
      <% end %>
      <th><%= "Student average" if group.eval_count > 0  %></th>
    </tr>
  </thead>
  <tbody>
    <% group.students.each do |student| %>
    <tr class="<%= cycle("odd", "even", name: "students")%>">
      <td>
        <%= link_to student.name, 
                    student_path({
                      student_group_id: student.student_group_id, 
                      id: student.id
                      })%>
      </td>
      <% group.eval_count.times do |i| %>
      <td class="center"><%= student.avg_for_eval(i+1) %></td>
      <% end %>
      <td class="center"><%= student.avg unless student.avg.nan? %></td>
    </tr>
    <% end %>
    <% reset_cycle("students") %>
  </tbody>
</table>

如果您不想遍历所有评估编号(即从 29 开始),那么使用 range 就像(min..group.eval_count).each您也可以在 reverse 中一样(min..max).to_a.reverse.each。从你的数据中找出maxmin应该是相当微不足道的。

学生.rb

def avg_for_eval(i)
  # nil evaluated as false hence this works...
  if evaluations_with_sum[i]
     (evaluations_with_sum[i].sum / evaluations_with_sum[i].size).round(2)
  else
    'no data'
  end  
end

private
def evaluations_with_sum
  # memoize as hash using instance variable
  # 'order' does work with symbol and the sorting will be ASC
  @evals ||= self.evaluations.order(:eval_number).inject({}) do |hash, evaluation|
    hash[evaluation.eval_number] ||= []
    hash[evaluation.eval_number] << evaluation.score
    hash
  end
end

实际上我认为self.evaluations.order(:eval_number).average(:score).group(:eval_number)应该给你一个带有eval_numberas 键和as值的哈希,score但我不太确定你可以尝试一下......</p>

于 2013-09-06T15:07:00.623 回答