# Listing all of a nested Resources

**URL:** <https://www.ruby-forum.com/t/listing-all-of-a-nested-resources/101234>\
**Category:** Rails\
**Created:** [August 3, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/listing-all-of-a-nested-resources/101234 "2006-08-03T18:15:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [August 3, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/listing-all-of-a-nested-resources/101234/1 "2006-08-03T18:15:48Z")

</div>

I’ll use the example on the Rails blog.

map.resources :posts do |posts|  
posts.resources :comments, :trackbacks  
end

Now comments are at /posts/:post\_id/comments.

Okay but what if I want to list all the comments for all the posts. It  
should be at /comments, but that isn’t map that way. Can I map comments  
twice?

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [August 3, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/listing-all-of-a-nested-resources/101234/2 "2006-08-03T19:16:49Z")

</div>

Josh P. wrote:

> I’ll use the example on the Rails blog.
> 
> map.resources :posts do |posts|  
> posts.resources :comments, :trackbacks  
> end
> 
> Now comments are at /posts/:post\_id/comments.
> 
> Okay but what if I want to list all the comments for all the posts. It  
> should be at /comments, but that isn’t map that way. Can I map comments  
> twice?

As I understand it (meaning untested by me), that route will generate  
two valid comment collection urls

1. /posts/:post\_id/comments
2. /comments

So I think if you call comments\_url and leave out the :post\_id, it will  
give you ‘/comments’. Then in your controller you can have a switch  
like:

def index  
if params[:post\_id]  
Comment.find(:all, :conditions =\> {:post\_id =\> params[:post\_id]})  
else  
Comment.find(:all)  
end  
end

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [August 3, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/listing-all-of-a-nested-resources/101234/3 "2006-08-03T19:46:00Z")

</div>

Alex W. wrote:

> As I understand it (meaning untested by me), that route will generate  
> two valid comment collection urls
> 
> 1. /posts/:post\_id/comments
> 2. /comments

Tested! Thats awesome.

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [August 3, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/listing-all-of-a-nested-resources/101234/4 "2006-08-03T20:28:52Z")

</div>

/comments/1 Doesn’t work.

You get Unknown action, No action responded to 1.

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [August 4, 2006, 2:32am UTC](https://www.ruby-forum.com/t/listing-all-of-a-nested-resources/101234/5 "2006-08-04T02:32:07Z")

</div>

Josh,

Yes, in that case you can simply add another route:

map.connect ‘comments/’, :controller =\> ‘comments’

Hope this helps,  
Zack

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [August 3, 2006, 7:49pm UTC](https://www.ruby-forum.com/t/listing-all-of-a-nested-resources/101234/6 "2006-08-03T19:49:39Z")

</div>

On 8/3/06, Josh P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> twice?  
> I think you could just do  
> map.resources :comments

in addition to what you have.

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [August 4, 2006, 3:16am UTC](https://www.ruby-forum.com/t/listing-all-of-a-nested-resources/101234/7 "2006-08-04T03:16:25Z")

</div>

Josh P. wrote:

> /comments/1 Doesn’t work.
> 
> You get Unknown action, No action responded to 1.

I’m going to avoid this approach because it creates another action that  
does the same thing as another. Not DRY.
