# Multiple Nested Resources and Routes

**URL:** <https://www.ruby-forum.com/t/multiple-nested-resources-and-routes/175536>\
**Category:** Rails\
**Created:** [September 15, 2009, 10:10am UTC](https://www.ruby-forum.com/t/multiple-nested-resources-and-routes/175536 "2009-09-15T10:10:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayoshi13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jayoshi13](https://www.ruby-forum.com/u/jayoshi13)\
**Post date:** [September 15, 2009, 10:10am UTC](https://www.ruby-forum.com/t/multiple-nested-resources-and-routes/175536/1 "2009-09-15T10:10:00Z")

</div>

Hi

A noob question on using routes with multiple nested resources -

The nested resources are as follows -

-\> Projects  
-\> Assessments  
-\>Findings

A Project has many Assessments, an Assessment has many Findings.

At presents it all works well for the following URL -

/projects/1/assessments/1/

Now that I have added the code to nest Findings in the associated  
Assessment I need to add the correct Route. At the moment  
(understandably) I get "Route Error’ when I browse to the following -

/projects/1/assessments/1/findings/1/

My routes.rb is currently configured as follows -

map.resources :projects, :has\_many =\> :assessments  
map.resources :assessments, :has\_many =\> :findings

What is the best route syntax for making this route work?

I also plan to add resolutions as nested resource to findings.

Thanks,

Jason

---

<div class="post-metadata">

**Author:** ![jayoshi13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jayoshi13](https://www.ruby-forum.com/u/jayoshi13)\
**Post date:** [September 15, 2009, 10:44am UTC](https://www.ruby-forum.com/t/multiple-nested-resources-and-routes/175536/2 "2009-09-15T10:44:43Z")

</div>

Hi  
Do like

in routes.rb  
map.resources :projects do |project|  
project.resources :assessments do |assessment|  
assessment.resources :findings  
end  
end

Now the helper for /projects/1/assessments/1/findings/1/

is project\_assessment\_finding\_url(projectid,assesmentid,findingid) it  
will map to show action of findings controller

Sijo

---

<div class="post-metadata">

**Author:** ![jayoshi13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jayoshi13](https://www.ruby-forum.com/u/jayoshi13)\
**Post date:** [September 15, 2009, 12:34pm UTC](https://www.ruby-forum.com/t/multiple-nested-resources-and-routes/175536/3 "2009-09-15T12:34:35Z")

</div>

On Sep 15, 3:44 am, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## in routes.rb map.resources :projects do |project| project.resources :assessments do |assessment| assessment.resources :findings end end

I’d consider adding :shallow =\> true. There is a lot of stuff out  
there about deeply nested routes (you don’t need them!). Three deep is  
not necessarily deep, but you only need two in any case (parent/child)  
and only one in a lot of cases (edit/show)

You really only need:  
/assessments/1/findings/1/  
Since assessment(1) knows who its parent is (project(1)).

It is strange that I found a post that seems to be trying to solve a  
similar real world problem with RoR and believe me it is a real  
learning experience. While I’ve be playing with rails for years I’ve  
only tried to develop a complex application recently. Consider:

map.resources :companies, :shallow =\> true do |company|  
company.resources :projects do |project|  
project.resources :jobs do |job|  
job.resources :stages do |stage|  
stage.resources :stage\_assessments  
stage.resources :assessments, :through  
=\> :stage\_assessments, :only =\> [:index]  
stage.resources :citizens, :through =\> :citizen\_stages  
stage.resources :citizen\_stages  
end  
end  
end  
end

and

map.resources :assessments, :member =\> {:post =\> :post, :display  
=\> :get, :clone =\> :get, :application =\> :get },:shallow =\> true do |  
q|  
q.resources :questions, :member =\> {:duplicate =\> :get}, :shallow  
=\> true do |a|  
a.resources :answers  
end  
end

And this is just a draft version - there is even more! This is really  
a dual application (maybe should be two). One side is project  
management where the customers projects consist of finding people  
(citizens) for jobs. The citizens go through a number of stages/phases  
(application, interview, training, etc). Each stage can have multiple  
assessments. In other words it job applicant screening process and  
those with the best scores in a phase move on to the next phase. I  
should point out that the stages and assessments can be customized for  
each job. The assessments mapping allows you to build or clone  
assessments that can be customized.

I think my major problem was trying to build this application was  
experimenting! That is generating the CRUD resources and then trying  
to define the resource route mapping. I should have started with the  
routes and defined “how” I was going to do things (the flow).

Steve
