我有一种方法可以计算给定记录集的平均值:
input = params[:recommendation_ratings].values # The params are sent from radio_tags in my view.
input.each do |mini_params|
rating_id = mini_params[:rating_id]
l = Rating.find(rating_id) #Find record on Rating table.
l.rating #Get value associated with rating_id
total_rating = []
total_rating << l.rating
average = total_rating.inject{ |sum, el| sum + el }.to_f / total_rating.size
puts average
end
l.rating 未附加到 total_rating 数组。看跌平均值打印为:
3.0
3.0
3.0
3.0
3.0
如何将返回的每个评级附加到数组中以计算平均值和其他数学函数。