# Moving to nested routes

**URL:** <https://www.ruby-forum.com/t/moving-to-nested-routes/191451>\
**Category:** Rails\
**Created:** [July 12, 2010, 11:20pm UTC](https://www.ruby-forum.com/t/moving-to-nested-routes/191451 "2010-07-12T23:20:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [July 12, 2010, 11:20pm UTC](https://www.ruby-forum.com/t/moving-to-nested-routes/191451/1 "2010-07-12T23:20:28Z")

</div>

I have a lot of has\_many and has\_many :through relationships in an  
application with approx 100 models. I would like to move to nested  
routes, but I just wanted to be sure I understood the scope of the  
project. Essentially, I see this as two tasks:

1.) modify config/routes.rb such that my relationships are expressed  
in nested route statements  
2.) modify every route reference in a link\_to etc to use the new  
nested route name (instead of new\_ip\_address, I’d have  
new\_network\_ip\_address etc.)

Is that about it?

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [July 13, 2010, 12:13am UTC](https://www.ruby-forum.com/t/moving-to-nested-routes/191451/2 "2010-07-13T00:13:33Z")

</div>

chewmanfoo wrote:

> I have a lot of has\_many and has\_many :through relationships in an  
> application with approx 100 models. I would like to move to nested  
> routes, but I just wanted to be sure I understood the scope of the  
> project. Essentially, I see this as two tasks:
> 
> 1.) modify config/routes.rb such that my relationships are expressed  
> in nested route statements  
> 2.) modify every route reference in a link\_to etc to use the new  
> nested route name (instead of new\_ip\_address, I’d have  
> new\_network\_ip\_address etc.)
> 
> Is that about it?

typically you would also have to slightly modify your controllers:  
instead of  
@ip\_address = IpAddress.new  
you should use  
@network = Network.find(params[:network\_id])  
@ip\_address = @network.ip\_adresses.build

and instead of  
@ip\_address = IpAddress.find(params[:id])  
use  
@network = Network.find(params[:network\_id])  
@ip\_address = @network.ip\_adresses.find(params[:id])

this isn’t really necessary, but this way rails will take care of the  
association (e.g. automatically assign :network\_id when you call build)

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [July 13, 2010, 12:15am UTC](https://www.ruby-forum.com/t/moving-to-nested-routes/191451/3 "2010-07-13T00:15:55Z")

</div>

and you may also have to modify forms, like form\_for [@network,  
@ip\_address]
