# Nested resources with RESTful Rails

**URL:** <https://www.ruby-forum.com/t/nested-resources-with-restful-rails/116663>\
**Category:** Rails\
**Created:** [October 8, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/nested-resources-with-restful-rails/116663 "2007-10-08T16:22:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 8, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/nested-resources-with-restful-rails/116663/1 "2007-10-08T16:22:24Z")

</div>

Hi,

I’m using RESTful in Rails, and I have an application with the  
following situation:

I have categories, articles and comments. A category can contain many  
articles (one-to-many relation) and an article can have many comments  
(one-to-many relation). This is like nested in nested resources. My  
question is how do I treat this in RESTful?

BTW, I’ve already implemented the code for categories and articles, I  
still have to add the comments and I’m not sure how. If I add  
comments, I’ll have to display the comments only inside the article  
show view and there shouldn’t be a view where I can only see some  
comments. Also, how do I integrate the comments\_controller::index  
action with the articles show view?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 8, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/nested-resources-with-restful-rails/116663/2 "2007-10-08T16:38:11Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi,
> 
> I’m using RESTful in Rails, and I have an application with the  
> following situation:
> 
> I have categories, articles and comments. A category can contain many  
> articles (one-to-many relation) and an article can have many comments  
> (one-to-many relation). This is like nested in nested resources. My  
> question is how do I treat this in RESTful?
> 
> BTW, I’ve already implemented the code for categories and articles, I  
> still have to add the comments and I’m not sure how. If I add  
> comments, I’ll have to display the comments only inside the article  
> show view and there shouldn’t be a view where I can only see some  
> comments. Also, how do I integrate the comments\_controller::index  
> action with the articles show view?

I would not think of an article as a nested resource within a category.  
I think of a nested resource as something that is tied to the lifecycle  
of the parent object. If you delete the parent, you also would want to  
delete the children. In this case, article would be the parent and  
comments would be the children because if you delete an article, you  
want to delete the comments. In the case of something like a blogging  
system, this would not be true of the category/article relationship and  
so I would not use a nested pattern there.

mike

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 8, 2007, 5:48pm UTC](https://www.ruby-forum.com/t/nested-resources-with-restful-rails/116663/3 "2007-10-08T17:48:08Z")

</div>

Hi, I recommend you to consult this paper by Google - “RESTful Rails  
Development”  
On the second part, it shows a pretty detailed example how nested  
resource could be handled probably - how to revise route.rb, etc.  
Hope this could resolve your puzzle.

Br,  
Xi

On 10ÔÂ8ÈÕ, ÏÂÎç5Ê±21·Ö, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 8, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/nested-resources-with-restful-rails/116663/4 "2007-10-08T18:11:01Z")

</div>

myst\_tt, you forgot to post the link. thanks 🙂

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 9, 2007, 10:05am UTC](https://www.ruby-forum.com/t/nested-resources-with-restful-rails/116663/5 "2007-10-09T10:05:24Z")

</div>

I’ve read this and I understand the RESTful concept. Though it’s not  
obvious that I shouldn’t make the categories resources.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 9, 2007, 3:14am UTC](https://www.ruby-forum.com/t/nested-resources-with-restful-rails/116663/6 "2007-10-09T03:14:26Z")

</div>

[http://www.google.com/url?sa=t&ct=res&cd=4&url=http%3A%2F%2Fwww.b-simple.de%2Fdocuments%2Fdownload%2F6&ei=qNUKR4G8Ip7ynAONrfWmBQ&usg=AFQjCNGNi84oAkjqPF42jgMvhyhN0bRibg&sig2=FHJY8wB74qbE3LXj\_JnUzw](http://www.google.com/url?sa=t&ct=res&cd=4&url=http%3A%2F%2Fwww.b-simple.de%2Fdocuments%2Fdownload%2F6&ei=qNUKR4G8Ip7ynAONrfWmBQ&usg=AFQjCNGNi84oAkjqPF42jgMvhyhN0bRibg&sig2=FHJY8wB74qbE3LXj_JnUzw)

On Oct 9, 12:10 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
