Monday, 15 February 2010

Ruby on Rails activerecord reputation system for average rating and total number of ratings -



Ruby on Rails activerecord reputation system for average rating and total number of ratings -

i using activerecord_reputation_system gem rate something, let's phone call model x. in app/models/x.rb have

has_reputation :rating, source: :user, aggregated_by: :average

this lets me view average rating.

my question: how can view both average rating , total number of votes model x? 1 way add together app/models/x.rb:

has_reputation :rating, source: :user, aggregated_by: :average has_reputation :reviews, source: :user, aggregated_by: :sum

and in controller have:

value = params[:rating] @x = x.find(params[:id]) if x.where(id: params[:id]).evaluated_by(:reviews, current_user).blank? @x.add_or_update_evluation(:reviews, 1, current_user) end @x.add_or_update_evaluation(:rating, value, current_user)

is best way or there improve way?

thanks lot.

i think can in way :

class vote < activerecord::base belongs_to :x attr_accessible :rate #it can in range 1..5 def rating rate end end class x < activerecord::base has_many :votes def average_rating unless votes.empty? (self.votes.sum(&:rating)/number_of_votes.to_f).round end end def number_of_votes self.votes.count end end

also can utilize gem: https://github.com/muratguzel/letsrate

let me know if solution .

ruby-on-rails activerecord reputation-tracker

No comments:

Post a Comment