# How to keep track of popular items

**URL:** <https://www.ruby-forum.com/t/how-to-keep-track-of-popular-items/157471>\
**Category:** Rails\
**Created:** [January 12, 2009, 1:00am UTC](https://www.ruby-forum.com/t/how-to-keep-track-of-popular-items/157471 "2009-01-12T01:00:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [January 12, 2009, 1:00am UTC](https://www.ruby-forum.com/t/how-to-keep-track-of-popular-items/157471/1 "2009-01-12T01:00:53Z")

</div>

I have a site which is basically a catalog of product about 30,000.00  
products. What do you think is the best (in terms of performance) way to  
keep track of the most popular products,a part from the idea of writing  
web-server log parser.

I’m thinking of creating a model called ‘product\_view’ which has  
product\_id & view\_counter, and updating this table in product#show.

---

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [January 12, 2009, 1:40am UTC](https://www.ruby-forum.com/t/how-to-keep-track-of-popular-items/157471/2 "2009-01-12T01:40:44Z")

</div>

You could just keep it in memory if accuracy isn’t very important.

On Sun, Jan 11, 2009 at 7:00 PM, Mohammad A. \<

---

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [January 12, 2009, 1:42am UTC](https://www.ruby-forum.com/t/how-to-keep-track-of-popular-items/157471/3 "2009-01-12T01:42:30Z")

</div>

I think I might look into this

[http://playtype.net/past/2008/2/6/starling\_and\_asynchrous\_tasks\_in\_ruby\_on\_rails/](http://playtype.net/past/2008/2/6/starling_and_asynchrous_tasks_in_ruby_on_rails/)

---

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [January 12, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/how-to-keep-track-of-popular-items/157471/4 "2009-01-12T15:17:24Z")

</div>

Popular in terms of viewing or selling?

Why don’t you keep a counter in the DB?

Cheers, Sazima

On Jan 11, 10:00 pm, Mohammad A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [January 12, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/how-to-keep-track-of-popular-items/157471/5 "2009-01-12T16:00:09Z")

</div>

Mohammad A. wrote:

> I’m thinking of creating a model called ‘product\_view’ which has  
> product\_id & view\_counter, and updating this table in product#show.  
> This sounds like a fine plan, but does it provide any advantage over  
> simply adding a view\_count attribute to your product model?

In either case you must update the database on every product view.  
Putting the column right on the product model seems to be a simpler  
solution to me. This way you don’t have to mess with any model  
associations.

Having a separate table to store your counts would, technically, be a  
more efficient use of disk space. But, as you said you expect around  
30,000 products. This is a pretty small number in the grand scheme of  
things so I wouldn’t be worried much about the extra space one more  
integer column would take. Certainly not enough to warrant the addition  
of a separate model.
