0
我有以下几列的examscore模型一个Fixnum循环:通过在轨
academic_year_id,grade_id,student_id数据,subject_id,total_score
,并用这些的averagescore模型列:
academic_year_id ,grade_id,student_id数据,平均
我的要求是由academic_year_id
和grade_id
通过Examscore
模型迭代,并计算出每个学生由学生所采取的所有科目的平均分(即总结所有科目的总成绩除以受试者的数量),然后将该平均值存储在AverageScore
模型中。我发现这很困难,任何帮助将非常感激。
我迄今所做的是:
total = 0
counter = 0
scores = Examscore.where(:academic_year_id => 3, :grade_id => 4)
scores.each do |std|
std.student_id.each do |tot|
total += tot.total_score
counter += 1
end
puts total.fdiv(counter)
但它失败,因为std.student_id
是Fixnum
不是Array
。
任何关于如何处理这个问题的指导?
这样做的伎俩。干杯 –