title: Rails 4 [Best practices] Nested resources and shallow: true
body: <p>The following post is based on Rails 4.</p>
  <p>I am currently looking for a best-practice about the multiple nested resources (more than 1), and the option shallow: true.</p>
  <p>Initially in my routes, there was this :</p>
  <pre><code>resources :projects do 
    resources :collections
  end
  </code></pre>
  <p>The associated routes are :</p>
  <pre><code>    project_collections GET    /projects/:project_id/collections(.:format)          collections#index
                          POST   /projects/:project_id/collections(.:format)          collections#create
   new_project_collection GET    /projects/:project_id/collections/new(.:format)      collections#new
  edit_project_collection GET    /projects/:project_id/collections/:id/edit(.:format) collections#edit
       project_collection GET    /projects/:project_id/collections/:id(.:format)      collections#show
                          PATCH  /projects/:project_id/collections/:id(.:format)      collections#update
                          PUT    /projects/:project_id/collections/:id(.:format)      collections#update
                          DELETE /projects/:project_id/collections/:id(.:format)      collections#destroy
                 projects GET    /projects(.:format)                                  projects#index
                          POST   /projects(.:format)                                  projects#create
              new_project GET    /projects/new(.:format)                              projects#new
             edit_project GET    /projects/:id/edit(.:format)                         projects#edit
                  project GET    /projects/:id(.:format)                              projects#show
                          PATCH  /projects/:id(.:format)                              projects#update
                          PUT    /projects/:id(.:format)                              projects#update
                          DELETE /projects/:id(.:format)                              projects#destroy
  </code></pre>
  <p>I read in the <a href="http://guides.rubyonrails.org/routing.html#limits-to-nesting" rel="nofollow noreferrer">documentation</a> about the limitation of nested resources :</p>
  <blockquote>
  <p>Resources should never be nested more than 1 level deep.</p>
  </blockquote>
  <p>Ok. Then, like the documentation said, I'm gonna use &quot;shallow&quot; in my routes instead.</p>
  <pre><code>shallow do
    resources :projects do 
      resources :collections
    end
  end
  </code></pre>
  <p>The associated routes are :</p>
  <pre><code>   project_collections GET    /projects/:project_id/collections(.:format)     collections#index
                         POST   /projects/:project_id/collections(.:format)     collections#create
  new_project_collection GET    /projects/:project_id/collections/new(.:format) collections#new
         edit_collection GET    /collections/:id/edit(.:format)                 collections#edit
              collection GET    /collections/:id(.:format)                      collections#show
                         PATCH  /collections/:id(.:format)                      collections#update
                         PUT    /collections/:id(.:format)                      collections#update
                         DELETE /collections/:id(.:format)                      collections#destroy
                projects GET    /projects(.:format)                             projects#index
                         POST   /projects(.:format)                             projects#create
             new_project GET    /projects/new(.:format)                         projects#new
            edit_project GET    /projects/:id/edit(.:format)                    projects#edit
                 project GET    /projects/:id(.:format)                         projects#show
                         PATCH  /projects/:id(.:format)                         projects#update
                         PUT    /projects/:id(.:format)                         projects#update
                         DELETE /projects/:id(.:format)                         projects#destroy
  </code></pre>
  <p>The major difference I see is the &quot;show&quot; of collections, this specific one :</p>
  <pre><code>collection GET    /collections/:id(.:format)                      collections#show
  </code></pre>
  <p>So if I I'm correct, the link for the show action for a collection is :</p>
  <pre><code>&lt;%= link_to 'Show&quot;, collection_path(collection)%&gt;
  </code></pre>
  <p>and should return something like this : &quot;http://example.com/collections/1&quot;</p>
  <p>BUT ! 2 things :</p>
  <ul>
  <li>This is not working. I'm getting instead &quot;http://example.com/projects/1&quot;.</li>
  <li>Even if it was working, it's actually IMO pretty bad because I loose the REST basic that say &quot;Collection is child of project, then the url should be &quot;localhost/project/1/collections/1&quot;</li>
  </ul>
  <p>I don't understand what is the interest of shallow if I loose the big advantage of Rest actions. And what is the interest to loose the &quot;Show&quot; action as well ? I already posted this to SO, but the only comment i got is &quot;It's something normal&quot;. I don't believe this is a normal behavior to &quot;remove&quot; an action from the rest API ?</p>
  <p>Yes, it might be convenient for the helpers to use shallow, but it is NOT AT ALL convenient for the rest, you loose all the interest of &quot;one collection is nested to one project, so this is reflected in the URL&quot;.</p>
  <p>I don't know if there is another way to do this, it's true that shallow allow more flexibility about the helpers, but it's false that it is REST compliant. So, is there any chance to get the &quot;helpers&quot; working (it's pretty awesome to have &quot;nested3_path(collection)&quot; instead of &quot;nested1_nested2_nested3([nested1.nested2.nested3, nested1.nested2, nested1])&quot;, and keeping the &quot;url part &quot;nested1/123/nested2/456/nested3/789&quot; ?</p>