# Nested resources and \_path methods

**URL:** <https://www.ruby-forum.com/t/nested-resources-and-path-methods/121757>\
**Category:** Rails\
**Created:** [November 30, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/nested-resources-and-path-methods/121757 "2007-11-30T16:13:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonas\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jonas\_s](https://www.ruby-forum.com/u/jonas_s)\
**Post date:** [November 30, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/nested-resources-and-path-methods/121757/1 "2007-11-30T16:13:57Z")

</div>

Hi guys,

I was thinking aboiut this for a while:

If I have nested resource routes, for example /building/1/floor/2/  
room/  
3 (building has\_many floors and so on), I have to call the room\_path  
method like this:

room\_path @room.floor.building, @room.floor, @room

Isn´t there a better way to handle nested routes with foreign keys, or  
can I have this method overwritten somewhere?

Greets  
Jonas

---

<div class="post-metadata">

**Author:** ![jonas\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jonas\_s](https://www.ruby-forum.com/u/jonas_s)\
**Post date:** [November 30, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/nested-resources-and-path-methods/121757/2 "2007-11-30T16:50:00Z")

</div>

i don’t understand what’s the exact question here, or how you would like  
this to be simplyfied, but the correct syntax should be:

building\_floor\_room\_path(building\_id, floor\_id, room\_id)

(at least for rails edge)

---

<div class="post-metadata">

**Author:** ![jonas\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jonas\_s](https://www.ruby-forum.com/u/jonas_s)\
**Post date:** [November 30, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/nested-resources-and-path-methods/121757/3 "2007-11-30T20:13:05Z")

</div>

On 30 Nov., 16:50, Thorsten M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> building\_floor\_room\_path(building\_id, floor\_id, room\_id)

Oh, of course, you´re right.

But what I meant is, that you have to specify every single item in the  
path of the url.  
I think that in 80% of the use cases, nested resources are used to  
indicate parenting in a tree-like data scheme.  
Perhaps there could be a new option in the map.resource method that  
could specify the foreign key to be used when generating a path or  
url, so you just have to write

building\_floor\_room\_path(room\_id)

and the values for building\_id and floor\_id would be automatically  
guessed out of the context.

Greets  
Jonas
