edgarjs / ajaxful-rating

Provides a simple way to add rating functionality to your application.

Home Page:http://rdoc.info/projects/edgarjs/ajaxful-rating

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Average across dimensions

angela1111 opened this issue · comments

Hi,

I really need to implement an average across all three dimensions... in a hurry. I don't know how to modify the code to do this. Would it be possible to walk me though calculating, caching and accessing an overall average across dimensions. It would be MUCH appreciated.

Thanks

Angela

It's nothing to do with the plugin, you can do maths directly on your columns. That's something like: sum all rates.rate columns and divide by the count of them. If you see the rates table you'll get a better idea.

When do you update the average column? How do you create an averages column?

The average calculation is in the method rate_average And the cached column update is in update_cached_average. All in the axr/model.rb file. You just need to read the code.

Bit slow to the party, but using a before_save callback sorted me out: -

class Beer < ActiveRecord::Base
  before_save :recalculate_rating_average

  def recalculate_rating_average
    rating_average = 0.0
    dimensions = self.class.ajaxful_rating_options[:dimensions]
    dimensions.each do |dimension|
      rating_average += self.send("rating_average_#{dimension}")
    end
    self.rating_average = rating_average / dimensions.size
  end
end