3

I've a model called Rating that reference itself.

class Rating < ActiveRecord::Base
  has_many :ratings, :as => :ratable, :dependent => :destroy
  after_save :update_total_rating
  private
  def update_total_rating
    return true unless value_changed?
    rating_sum = Rating.sum('value',
        :conditions =>["ratable_id = ? and ratable_type = ?",
                       self.ratable_id, self.ratable_type])
    ratable.update_attribute(:total_rating, rating_sum)
    return true
  end
end

I am doing this to cache the total rating on a rating. This is working perfectly on development and updating the total_rating, it's also working on production when I do a model save on rating in Heroku Rails Console (heroku run rails c), but this is not working when executed through the web app hosted on Heroku, it seems that update_total_rating is not being called for some reason.

Any idea what could be causing this or how to fix this?

Thanks

  • 2
    make sure that it really passes through the method. Add a log inside that method to see if Rails calls it. Remember that rails doesn't trigger callbacks when nothing has changed on the record. – jvnill Feb 01 '13 at 07:15

0 Answers0