# Rating/Survey Plugin

**URL:** <https://www.ruby-forum.com/t/rating-survey-plugin/51920>\
**Category:** Rails\
**Created:** [January 9, 2006, 8:59am UTC](https://www.ruby-forum.com/t/rating-survey-plugin/51920 "2006-01-09T08:59:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![etschreiber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a5b964/32.png) [@etschreiber](https://www.ruby-forum.com/u/etschreiber)\
**Post date:** [January 9, 2006, 8:59am UTC](https://www.ruby-forum.com/t/rating-survey-plugin/51920/1 "2006-01-09T08:59:36Z")

</div>

I would like include the ability for people to rate posts on my site  
from 1 to 5. I would like to then display the average rating for each  
post. To compute the average each time is obviously database and cpu  
intensive. I have been thinking about the best solution. I could  
certainly use a cache and expire it every x minutes. However, I don’t  
want to reinvent the wheel. Is there already a ROR plugin solution out  
there that I don’t know about? If not, does anyone have any good links  
on this subject?

Thanks for all of your time and effort on this great project.

Ethan Schreiber

---

<div class="post-metadata">

**Author:** ![etschreiber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a5b964/32.png) [@etschreiber](https://www.ruby-forum.com/u/etschreiber)\
**Post date:** [January 9, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/rating-survey-plugin/51920/2 "2006-01-09T17:18:44Z")

</div>

You probably should store the average (A) and number of votes (N) for  
each  
post. Then, to display the rating of a post, you query for A in the  
database. When a new rating is submitted, you compute A and you  
increment N.

Note: of course, A must be stored in float (or whatever) and rounded for  
display 😉

Does this help?

---

<div class="post-metadata">

**Author:** ![etschreiber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a5b964/32.png) [@etschreiber](https://www.ruby-forum.com/u/etschreiber)\
**Post date:** [January 9, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/rating-survey-plugin/51920/3 "2006-01-09T23:29:50Z")

</div>

Nicolas B. wrote:

> You probably should store the average (A) and number of votes (N) for  
> each  
> post. Then, to display the rating of a post, you query for A in the  
> database. When a new rating is submitted, you compute A and you  
> increment N.
> 
> Note: of course, A must be stored in float (or whatever) and rounded for  
> display 😉
> 
> Does this help?

Thank you for your response.

Yes that is one solution that would work. However, then I would be  
keeping track of individual votes as well as the total. (I need the  
individual votes as well.) It seems redundant. On top of that, I need  
to worry about the transaction of modifying the average column which can  
run into multi-threading problems. I was looking into memory cache  
solutions where i can compute the average and store it in a cache and  
perhaps invalidate the cache ever 10 or 15 minutes. This lagtime would  
be acceptable.

Is memcached the cache solution i should be using with ROR?
