# Top 10/most popular lists

**URL:** <https://www.ruby-forum.com/t/top-10-most-popular-lists/189107>\
**Category:** Rails\
**Created:** [May 24, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/top-10-most-popular-lists/189107 "2010-05-24T18:43:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 24, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/top-10-most-popular-lists/189107/1 "2010-05-24T18:43:19Z")

</div>

What’s the best way to handle views that show the top 10/most popular/  
Recently updated/most commented type listing?

Should I have a separate model that is populated with records from  
other models by a background job or should I need to use some sort of  
caching? Perhaps both?

Thanks!

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 25, 2010, 3:07am UTC](https://www.ruby-forum.com/t/top-10-most-popular-lists/189107/2 "2010-05-25T03:07:25Z")

</div>

Check out the :counter\_cache option of belongs\_to. If you order by  
that and limit to 10, you’ll probably have what you’re after.

/Ritchie

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 25, 2010, 4:16am UTC](https://www.ruby-forum.com/t/top-10-most-popular-lists/189107/3 "2010-05-25T04:16:29Z")

</div>

Thanks!

Let me clarify.

On my home page, I would like to show the following

top 10 clubs in your area  
most popular comments in your area  
popular events in your area…  
.  
.  
and so on.

This view will essentially show 10-15 records from 7-8 models. Which  
is a lot of queries and of course, its the home page, which I wanna  
make sure loads fast.

What’s the best way to optimize it?

Should I have another model called popular with colums names such as  
popular\_events, popular\_clubs, etc and populate/depopulate that with a  
background job? or is there a better way?

Thanks

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 25, 2010, 11:20pm UTC](https://www.ruby-forum.com/t/top-10-most-popular-lists/189107/4 "2010-05-25T23:20:59Z")

</div>

Folks, any thoughts?

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 26, 2010, 10:12am UTC](https://www.ruby-forum.com/t/top-10-most-popular-lists/189107/5 "2010-05-26T10:12:24Z")

</div>

On 25 May 2010 03:15, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> .  
> and so on.
> 
> This view will essentially show 10-15 records from 7-8 models. Which  
> is a lot of queries and of course, its the home page, which I wanna  
> make sure loads fast.
> 
> What’s the best way to optimize it?

I would not worry about optimising it until it becomes clear that it  
is going to be a problem. It is generally true that the bottlenecks  
in an app are not where you expect them to be. Optimising before you  
know exactly where the issues are is a waste of time. Make sure you  
have full test coverage so that when (if) you need to refactor you can  
be confident that all is still working.

Colin

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 26, 2010, 10:23am UTC](https://www.ruby-forum.com/t/top-10-most-popular-lists/189107/6 "2010-05-26T10:23:32Z")

</div>

You can also have a background job that queries all models for top 10  
results and save them into top\_10 table. This job may refresh the  
top\_10 table once a day for example (late at night?).  
This way your home page should not perform a lot of expensive queries  
for each visitor.  
As Colin L. said optimization should be done when it becomes necessary.

regards

2010/5/25 badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> .  
> background job? or is there a better way?
> 
> > On May 25, 12:42 am, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > –
> 
> –  
> You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid.  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

## –

Oliver Hernàndez Valls

[http://codit.wikidot.com](http://codit.wikidot.com)  
[http://wiki.tramuntanal.cat](http://wiki.tramuntanal.cat)
