# acts_as_rateable implementation / algorithm question

Hi there. I've been using acts_as_rateable on goneraw.com, which is
written in rails. It works great, but there's kind of a problem with
the way it sorts ratings. Namely, if everyone adores a recipe, and it's
been rated two dozen times at five-stars, but someone has given it four
stars, and you have another recipe with *one* rating of five stars...
the recipe with a single five-star rating will always come out on top!

solution in mind. Here's how I find the three favorite recipes (on the

@favorite_recipes = @recipes.sort { |a,b| b.rating <=> a.rating
}[0...3]

Well, it's not trivial to fix this, is it? Does anyone have a better
algorithm in mind, for weighting the ratings by how many ratings a
particular item has received? This seems like a common problem for
anyone using acts_as_rateable....

Sometimes simple works… I don’t use the acts_as_rateable plugin, but for the ‘Top Rated’ category at the plugin directory I simply sort by the rating times the number of votes.

Benjamin Curtis wrote:

Sometimes simple works... I don't use the acts_as_rateable plugin, but for the 'Top Rated' category at the plugin directory I simply sort by the rating times the number of votes.

Shouldn't one star be treated as a negative review? I'd rather choose a product with ten reviews that all give five stars, than a product with a thousand one-star reviews!

Justin Forder

It would seem that a simple adjustment to Ben's system would be to
slide the star ratings down by 3 before doing the calculation. Make 1
star => -2, 2 => -1, 3 => 0, 4 => 1, 5 => 2. Now, 1000 1 star reviews
will actually bring the total rating down.

You could adjust the weights to make 1 and 5 star ratings not be linear
- 1 star => -5, 5 star => 5

Earlier in this thread Tom Fakes recommended an improvement to what I described that solves the problem you mention.

Practically speaking, though, even my simplistic approach is suitable for the usage patterns of the plugin directory.

Benjamin Curtis wrote:

Sometimes simple works... I don't use the acts_as_rateable plugin,
but for the 'Top Rated' category at the plugin directory I simply
sort by the rating times the number of votes.

--
Building an e-commerce site with Rails?
http://www.agilewebdevelopment.com/rails-ecommerce

How would this work? If I have an item with 20 votes all of 1 star that
would equal 20*1 = 20

and If I have an item with 2 votes of 4 stars that would equal 2*4 = 8

so that item that had been with all 1-star votes would show up higher
than the item with all 4-star votes.

You could modify things so when some rates an object you do:

.. save ...

Then just sort by rating directly...

Interesting, I'm sorry I missed all the follow-ups on this thread!

For the record, what I ended up doing was taking the sum of all the
ratings, adding the number of ratings minus one to that number, then
dividing that total by the number of ratings. So a single, five-star
rating gives a result of 5, as does a recipe rated with a four-star and
five-star rating. Two five-star ratings gives 5.5, three five-stars
gives you 5.66, and so on. This was a weird solution, but it works fine
for my goal of keeping recipes with a single five-star rating from
showing up at the top of my list.