top 10/most popular lists

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!

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

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

Folks, any thoughts?

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?

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

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 Law said optimization should be done when it becomes necessary.

regards