# Nested resources

**URL:** <https://www.ruby-forum.com/t/nested-resources/152301>\
**Category:** Rails\
**Created:** [November 1, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/nested-resources/152301 "2008-11-01T16:36:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [November 1, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/nested-resources/152301/1 "2008-11-01T16:36:22Z")

</div>

Hello,

I have a problem with nested resources.

Ich made following entries in the routes.rb:

map.resources :projects do |project|  
project.resources :iterations do |iteration|  
iteration.resources :tasks  
end  
end

when I use link\_to:

\<%= link\_to ‘Show’, project\_iteration\_task\_path(task.iteration.project,  
task) %\>

following is displayed:

[http://localhost:3000/projects/2/iterations/117/tasks/32](http://localhost:3000/projects/2/iterations/117/tasks/32)

The problem is, that the IDs of iterations and task are interchanged.  
The real iteration\_id = 32 and the real task\_id = 117.

do sombebody knows a solution or what’s worn with the code?

Thanks for every answer!

Hermann

---

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [November 1, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/nested-resources/152301/2 "2008-11-01T17:36:49Z")

</div>

Herman MÃ¼ller schreef:

> end  
> The problem is, that the IDs of iterations and task are interchanged.  
> The real iteration\_id = 32 and the real task\_id = 117.
> 
> do sombebody knows a solution or what’s worn with the code?
> 
> Thanks for every answer!
> 
> Hermann

Hermann

You can try the alternative syntax:

map.resources :tasks  
map.resources :iterations, :has\_many =\> [:tasks]  
map.resources :projects, :has\_many =\> [:iterations]

Rudi

---

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [November 1, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/nested-resources/152301/3 "2008-11-01T19:21:06Z")

</div>

isn’t it safer to use the models to define the relationships?

---

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [November 1, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/nested-resources/152301/4 "2008-11-01T20:20:06Z")

</div>

Hello Rudi,

I’ve got an answer form the german forum:

The solution from Thomas B.,

project\_iteration\_task\_path(task.iteration.project, task.iteration,  
task)

and it works fine:-)

I only wanted to generate better readable URLS like:

[http://localhost:3000/projects/2/iterations/36/tasks/140](http://localhost:3000/projects/2/iterations/36/tasks/140)

When I used

map.resources :projects, :has\_many =\> [:iterations]

map.resources :iterations, :has\_many =\> [:tasks]

the same URL for tasks looked like this:

[http://localhost:3000/iterations/36/tasks/140](http://localhost:3000/iterations/36/tasks/140)

but there is no conclusion to the project\_id.

Thank you for your answer

Regs,

Herman
