# Mapping resources twice?

**URL:** <https://www.ruby-forum.com/t/mapping-resources-twice/77399>\
**Category:** Rails\
**Created:** [November 27, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/mapping-resources-twice/77399 "2006-11-27T17:52:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@snowbug](https://www.ruby-forum.com/u/snowbug)\
**Post date:** [November 27, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/mapping-resources-twice/77399/1 "2006-11-27T17:52:38Z")

</div>

Named changed to protect the innocent.

So I have

map.resources :stories do |story|  
story.resources :comments  
end

This lets me have /story/1/comments or /story/1/comment/3.

If I want to still have access to plain old /comments to give me all  
comments across all stories do I need to have:

map.resources :comments

map.resources :stories do |story|  
story.resources :comments  
end

Is this allowed ? Can I map the same resource into two different  
contexts ?

Alan

---

<div class="post-metadata">

**Author:** ![snowbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@snowbug](https://www.ruby-forum.com/u/snowbug)\
**Post date:** [November 27, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/mapping-resources-twice/77399/2 "2006-11-27T22:38:39Z")

</div>

> map.resources :comments  
> map.resources :stories do |story|  
> story.resources :comments, :name\_prefix =\> “inner\_”  
> end
> 
> Now you’ll have:
> 
> comment\_url(@comment)  
> inner\_comment\_url(@story, @comment)

Hi David,

That looks like it’ll fit the bill perfectly.

map.resources :comments, :name\_prefix =\> “all\_”

map.resources :stories do |story|  
story.resources :comments, :name\_prefix =\> “story\_”  
end

Giving me all\_comments\_url (not likely to use singular here) and  
story\_comment\_url(@story, @comment)

Sweet.

Alan

---

<div class="post-metadata">

**Author:** ![snowbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@snowbug](https://www.ruby-forum.com/u/snowbug)\
**Post date:** [November 27, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/mapping-resources-twice/77399/3 "2006-11-27T18:32:10Z")

</div>

Hi –

On Mon, 27 Nov 2006, Alan F. wrote:

> This lets me have /story/1/comments or /story/1/comment/3.
> 
> Is this allowed ? Can I map the same resource into two different  
> contexts ?

The thing is, when you call resources, you create methods called  
comments\_url, comment\_url, etc. So if you create those methods twice,  
the second set take precedence.

There are a couple of things you can do. One is [untested so tweak if  
I’ve mistyped it]:

map.resources :comments  
map.resources :stories do |story|  
story.resources :comments, :name\_prefix =\> “inner\_”  
end

Now you’ll have:

comment\_url(@comment)  
inner\_comment\_url(@story, @comment)

You can also add the :name\_prefix to the plain comments resources, if  
you’d rather have it work that way.

Another thing you can do – but be warned; it’s very experimental, and  
also may or may not suit your schema – is use my Inferred Routes  
plugin. This allows you to do just the nested route, and then when  
you do:

comment\_url(@comment)

the plugin will infer:

comment\_url(@comment.story, @comment)

You can get the plugin at:  
[http://www.risleydale.net/svn/inferred\_routes/tags/0.1.1](http://www.risleydale.net/svn/inferred_routes/tags/0.1.1)

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
