1
我需要通過幾個模型上的視圖進行一些計算。例如:Rails Mongoid模型/視圖計算
class Teacher
include Mongoid::Document
has_many :students
end
class Student
include Mongoid::Document
belogns_to :teacher
field gold_stars, type: Integer
field silver_stars, type: Integer
field bronze_stars, type: Integer
end
讓我們老師的觀點我需要聚合的gold_stars,silver_stars和bronze_stars數說。在視圖中彙總值的最乾淨的方法是什麼?我猜我會使用after_update回調,但我不確定是否有更好的方法。
UPDATE
我要的是讓教師來顯示他的所有的學生有多少金色星星總共有,那麼銀,然後銅牌。
要彙總數星星面向全體學生belongs_to的老師或學生的總星的解決方案? – abhas