我在部分视图中有以下代码,以显示每个评估期的学生平均分数:
<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
返回以下内容(对于id
32 岁的学生):
{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>]
}