我試圖計算平均值(mean)評級基於以下模型關聯一個類別中的所有條目...模型關係問題
class Entry < ActiveRecord::Base
acts_as_rateable
belongs_to :category
...
end
class Category < ActiveRecord::Base
has_many :entry
...
end
class Rating < ActiveRecord::Base
belongs_to :rateable, :polymorphic => true
...
end
該評級模型是由acts as rateable插件處理,所以應課稅模型看起來像這樣...
module Rateable #:nodoc:
...
module ClassMethods
def acts_as_rateable
has_many :ratings, :as => :rateable, :dependent => :destroy
...
end
end
...
end
我該如何進行平均計算?這可以通過rails模型關聯來完成,還是必須使用SQL查詢?
我深知平均的方法,但我不知道如何將其應用於這種情況。我有興趣找到特定類別內所有條目的平均值(平均值)。 – crh
當然!完善!謝謝。我開始覺得我瘋了。 – crh