Skip to content

Instantly share code, notes, and snippets.

@mrbrdo
Created April 2, 2011 21:35
Show Gist options
  • Save mrbrdo/899929 to your computer and use it in GitHub Desktop.
Save mrbrdo/899929 to your computer and use it in GitHub Desktop.
scope :by_rating, :lambda do |order|
order ||= "ASC"
previous_scope = ?
all_scores = previous_scope.where("num_scores > ?", 0).sum('score')
num_of_all_scores = previous_scope.where("num_scores > ?", 0).sum('num_scores')
site_avg = all_scores.to_f / num_of_all_scores.to_f
site_avg = 0 if num_of_all_scores <= 0 # this is the reason I'd like to do it this way instead of the normal way
min_votes = 5
previous_scope.where("num_scores > ?", 0).order("((num_scores / (num_scores + #{min_votes})) * (score / num_scores) + (#{min_votes} / (num_scores + #{min_votes})) * #{site_avg}) #{order}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment