新しいコメントが追加されると、属性:average_rateを更新する必要があります。 は私がpage.rb_changed?メソッドcounter_cacheを使用する場合
def update_average_rate(comment)
if comments_count_changed?
write_attribute :average_rate, (comments.sum(:rate)/comments.count.to_f).ceil
end
end
にcomments.rb
belongs_to :page, :counter_cache => true
にし、page.rb
has_many :comments
after_save :update_average_rate
とupdate_average_rate方法を持っているが、それは仕事をdoesntの。私が行っているときには、update_average_rateメソッドで
raise comments_count_changed?.inspect
が出力されますが、comments_countが変更されます。私が間違っていることは何ですか?事前に感謝します
':before_save'フックを試してください。 –
試行しましたが、同じ結果( – Pavel