# Multi nested resources

**URL:** <https://www.ruby-forum.com/t/multi-nested-resources/112517>\
**Category:** Rails\
**Created:** [August 20, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/multi-nested-resources/112517 "2007-08-20T15:45:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@marc](https://www.ruby-forum.com/u/marc)\
**Post date:** [August 20, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/multi-nested-resources/112517/1 "2007-08-20T15:45:13Z")

</div>

Hi guys,just wondering if someone could enlighten me about this.

Is it possible to have multi nested resources like

map.resources :owners do |owner|  
owner.resources :dogs,  
:controller =\> ‘dogs’,  
:singular =\> ‘dog’,  
:name\_prefix =\> ‘owner\_’  
owner.resources :cats,  
:controller =\> ‘cats’,  
:singular =\> ‘cat’,  
:name\_prefix =\> ‘owner\_’  
end

Just having a bit of trouble with routes… thanks!

---

<div class="post-metadata">

**Author:** ![marc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@marc](https://www.ruby-forum.com/u/marc)\
**Post date:** [August 20, 2007, 5:48pm UTC](https://www.ruby-forum.com/t/multi-nested-resources/112517/2 "2007-08-20T17:48:40Z")

</div>

Jeremy is right – you can next as deeply as you like. However, the  
deeper you go the more difficult it becomes to create the routes.  
I’ve got one spot in a project that is nested two levels deep and the  
code gets a bit annoying.

AndyV

On Aug 20, 9:47 am, “Jeremy McAnally” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![marc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@marc](https://www.ruby-forum.com/u/marc)\
**Post date:** [August 20, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/multi-nested-resources/112517/3 "2007-08-20T19:25:52Z")

</div>

I had nested about five levels deep on a project before we realized it  
was stupid and we de-nested. The route generation looked something  
like this:

site\_section\_unit\_text\_activity\_file\_path(params[:site\_id],  
params[:section\_id], params[:unit\_id], params[:text\_id],  
params[:activity\_id], params[:id])

Ridiculous. 🙂

–Jeremy

On 8/20/07, AndyV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > You may be having trouble generating routes like I did when I first
> > 
> > On 8/20/07, Marc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > ```
> > > owner.resources :dogs,
> > > 
> > > ```
> > 
> > –[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)
> > 
> > My free Ruby e-book:[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)
> > 
> > My blogs:[http://www.mrneighborly.com/http://www.rubyinpractice.com/](http://www.mrneighborly.com/http://www.rubyinpractice.com/)
> 
> >

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![marc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@marc](https://www.ruby-forum.com/u/marc)\
**Post date:** [August 20, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/multi-nested-resources/112517/4 "2007-08-20T22:44:22Z")

</div>

Hmm but I’m not trying to nest too deeply. It’s more like having two  
equal nests? An Owner can have a dog as well as a cat (let’s not get  
into like polymorphism for this case first =). How do you denote that  
on the routes?

url path would be like:

owner/1/dog/1  
and  
owner/1/cat/1

On Aug 20, 11:47 pm, “Jeremy McAnally” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![marc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@marc](https://www.ruby-forum.com/u/marc)\
**Post date:** [August 20, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/multi-nested-resources/112517/5 "2007-08-20T15:56:15Z")

</div>

You may be having trouble generating routes like I did when I first  
stated playing with it. 🙂

To generate a route to, say, a dog, you’ll to provide something like  
this:

owner\_dog\_path(@owner\_id, @dog\_id)

Then Rails builds the URL off of the two id’s to produce something  
like [http://yourapp.com/owners/1/dogs/2](http://yourapp.com/owners/1/dogs/2) or what have you.

I suggest read the PDF mentioned here:

[http://www.rubyinside.com/restful-rails-development-pdf-released-392.html](http://www.rubyinside.com/restful-rails-development-pdf-released-392.html)

…and referring to this cheatsheet:

> **[REST-cheatsheet.pdf](https://topfunky.com/clients/peepcode/REST-cheatsheet.pdf)**
>
> 78.73 KB

…whenever you need it.

–Jeremy

On 8/20/07, Marc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> owner.resources :cats,
> :controller => 'cats',
> :singular => 'cat',
> :name_prefix => 'owner_'
> 
> ```
> 
> end
> 
> Just having a bit of trouble with routes… thanks!
> 
> >

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![marc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@marc](https://www.ruby-forum.com/u/marc)\
**Post date:** [August 20, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/multi-nested-resources/112517/6 "2007-08-20T22:54:23Z")

</div>

argh ignore what I have posted, I found out the error of my ways… I  
forgot to put the prefix when i specified like:

cat\_path

should have been

owner\_cat\_path. doh.

Thanks all =)
