RESTful design for statistics

Hello,

I’ve a design question about a RESTful Weblog application. I use the
db layout: http://erdbeere.net/media/db_layout.png . 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

On Aug 11, 6:49 am, Parity [email protected] wrote:

Hello,

I’ve a design question about a RESTful Weblog application. I use the
db layout:http://erdbeere.net/media/db_layout.png. 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 resource for each
statistic, but should I really create a controller for each of them?

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

/posts/popular

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:

/posts?subset=popular

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:
http://www.purpleworkshops.com/workshops/rest-and-web-services

softiesonrails.com - blog
purpleworkshops.com - training
pragprog.com/titles/cerailn - 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