RESTful design for statistics


I've a design question about a RESTful Weblog application. I use the db layout: . There is a controller for each model. Now I want to have something like "popular posts", "recent posts", "post with the most comments", "current users", "most active users", ...

If I understand Rest correctly, i should have a ressource for each statistic, but should I really create a controller for each of them?

Thanks, Patrick

Hi Patrick,

You don't need a controller for each resource in this case - as long as you can identify it by URL, then it's a resource. So, if you want, you can provide named urls that point to each resource:

map.resources :posts, :collection => { :popular => :get }

which would give you a url like


However, it means that your controller would need a custom 'popular' action --- which is not the end of the world, but I tend to look for alternatives before resorting to custom actions. So another approach would be to use urls like:


which is still RESTful, and are easily generated with:

link_to "Popular Posts", posts_path(:subset => 'popular')

You can then retrieve the appropriate posts in your index action depending on the value of params[:subset], etc.

Jeff Come to REST with Rails, Oct 4, 2008 in Austin, TX: - blog - training - Upcoming book, Rails for .NET Developers

Thank you, Jeff. It is really helpful for me. I think I have to learn a lot to use REST and Rails, but a workshop in TX is much to far away from europe :wink:

Thank you very much and have a nice day, Patrick