# Acts\_as\_rateable implementation / algorithm question

**URL:** <https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271>\
**Category:** Rails\
**Created:** [November 25, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271 "2006-11-25T20:33:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![raymond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@raymond](https://www.ruby-forum.com/u/raymond)\
**Post date:** [November 25, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271/1 "2006-11-25T20:33:38Z")

</div>

Hi there. I’ve been using acts\_as\_rateable on [goneraw.com](http://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!

I’ve been thinking a lot about this problem and don’t have a clean  
solution in mind. Here’s how I find the three favorite recipes (on the  
home page) now:

@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…

---

<div class="post-metadata">

**Author:** ![raymond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@raymond](https://www.ruby-forum.com/u/raymond)\
**Post date:** [November 26, 2006, 4:38am UTC](https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271/2 "2006-11-26T04:38:11Z")

</div>

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](http://www.agilewebdevelopment.com/rails-ecommerce)

---

<div class="post-metadata">

**Author:** ![raymond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@raymond](https://www.ruby-forum.com/u/raymond)\
**Post date:** [November 26, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271/3 "2006-11-26T20:25:18Z")

</div>

Benjamin C. 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 F.

---

<div class="post-metadata">

**Author:** ![raymond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@raymond](https://www.ruby-forum.com/u/raymond)\
**Post date:** [November 26, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271/4 "2006-11-26T20:47:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raymond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@raymond](https://www.ruby-forum.com/u/raymond)\
**Post date:** [December 7, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271/5 "2006-12-07T18:21:58Z")

</div>

On Dec 7, 2006, at 8:53 AM, Craig J. wrote:

> 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.

Earlier in this thread Tom F. 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.

–  
Building an e-commerce site with Rails?  
[http://www.agilewebdevelopment.com/rails-ecommerce](http://www.agilewebdevelopment.com/rails-ecommerce)

---

<div class="post-metadata">

**Author:** ![raymond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@raymond](https://www.ruby-forum.com/u/raymond)\
**Post date:** [December 7, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271/6 "2006-12-07T18:54:10Z")

</div>

> 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:

rating = (rating \* number\_of\_votes + new\_rating) / (number\_of\_votes + 1)  
number\_of\_votes += 1  
… save …

Then just sort by rating directly…

---

<div class="post-metadata">

**Author:** ![raymond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@raymond](https://www.ruby-forum.com/u/raymond)\
**Post date:** [December 7, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271/7 "2006-12-07T17:53:18Z")

</div>

Benjamin C. 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](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.

---

<div class="post-metadata">

**Author:** ![raymond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@raymond](https://www.ruby-forum.com/u/raymond)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/acts-as-rateable-implementation-algorithm-question/77271/8 "2007-01-06T19:30:13Z")

</div>

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.
