# N-levels of nested resources in REST?

**URL:** <https://www.ruby-forum.com/t/n-levels-of-nested-resources-in-rest/131301>\
**Category:** Rails\
**Created:** [March 7, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/n-levels-of-nested-resources-in-rest/131301 "2008-03-07T20:28:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 7, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/n-levels-of-nested-resources-in-rest/131301/1 "2008-03-07T20:28:04Z")

</div>

Hi,

I find it would be very convenient to nest resources in two levels, so  
a member is only sensible as a part of a club, and a fee pament is  
only sensible in the context of a club and a member.  
Is it possible to nest nested resources? How would the syntax be?

I have tried just nesting the assignment in routes.rb, but failed to  
get it right

What do ou think? Is it possible?

/Fredrik

–  
“Give up learning, and put an end to your troubles.”

---

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 7, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/n-levels-of-nested-resources-in-rest/131301/2 "2008-03-07T20:36:23Z")

</div>

map.resources :foos do |foo|  
foo.resources :bars do |bar|  
bar.resources :bazs  
end  
end

---

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 7, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/n-levels-of-nested-resources-in-rest/131301/3 "2008-03-07T20:40:02Z")

</div>

Alternatively:

map.resources :foos do |foo|  
foo.resources :bars, :has\_many =\> :bazs  
end

---

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 8, 2008, 8:57pm UTC](https://www.ruby-forum.com/t/n-levels-of-nested-resources-in-rest/131301/4 "2008-03-08T20:57:58Z")

</div>

+1. It’s been widely discussed elsewhere and many of the insiders  
suggest not nesting more than 2 levels deep.

---

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 8, 2008, 11:50am UTC](https://www.ruby-forum.com/t/n-levels-of-nested-resources-in-rest/131301/5 "2008-03-08T11:50:35Z")

</div>

Hi Fredrik,

Just a small warning from my own experience: nesting deeper than 1  
level is not advised.  
It will become very tedious to keep supplying the unneeded lower  
levels of nesting in the url. If you know the “bar”, you implicitly  
also know the “foo”. So there is really no need to keep suppling the  
“foo”, “bar” & “baz” all the time.

I generally use the following nesting setup:

map.resources :foos do |foo|  
foo.resources :bars  
end

map.resources :bars do |bar|  
bar.resources :bazs  
end

map.resources :bazs

This way your routes to the “baz” don’t require a “foo”.  
Also, if you know the “baz” resource, you can always create a direct  
route. For the index, new & create actions on “baz”, you can the  
choose the route which also requires the “bar” to define the scope.

Regards, Bas

On Mar 7, 8:40 pm, Gerjan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
