# RESTful design for statistics

**URL:** <https://www.ruby-forum.com/t/restful-design-for-statistics/145549>\
**Category:** Rails\
**Created:** [August 11, 2008, 1:50pm UTC](https://www.ruby-forum.com/t/restful-design-for-statistics/145549 "2008-08-11T13:50:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [August 11, 2008, 1:50pm UTC](https://www.ruby-forum.com/t/restful-design-for-statistics/145549/1 "2008-08-11T13:50:43Z")

</div>

Hello,

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

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [August 11, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/restful-design-for-statistics/145549/2 "2008-08-11T16:34:24Z")

</div>

On Aug 11, 6:49 am, Parity [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I’ve a design question about a RESTful Weblog application. I use the  
> db layout:[http://erdbeere.net/media/db\_layout.png](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](http://www.purpleworkshops.com/workshops/rest-and-web-services)

[softiesonrails.com](http://softiesonrails.com) - blog  
[purpleworkshops.com](http://purpleworkshops.com) - training  
[pragprog.com/titles/cerailn](http://pragprog.com/titles/cerailn) - Upcoming book, Rails for .NET Developers

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [August 11, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/restful-design-for-statistics/145549/3 "2008-08-11T16:39:06Z")

</div>

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 😉

Thank you very much and have a nice day,  
Patrick
