Nested Routes Rails precautions

Resources should never be nested more than 1 level deep.

These multi-level nested routes is violation of above mentioned rule.

resources :hotels do
  resources :food_menus do
   resources :categories do
     resources :dishes

My Questions about Rails Guide Recommendation:
1. Why it is recommended to use 1 Level Deep routes?

2. Why they are saying not to enjoy Rails Nested Routes?

3. Is there any problem or overhead there?

4. One Level Nesting would cause multiple generation of a resource routes
    a. One as Nested
    b. Other as Non Nested

5. So On… 🙂

Answer to Q# 1,2,3 is:
If you use more than 1 level deep nesting then,

1. You need to pass object of each resource in helper

2. Rails will generate very very long Routes Helper & Url.

Show Method Url & Helper of dishes:

hotel_food_menu_category_dish_path(@hotel_object, @food_menu_object, @categor_object, @dish_object )

3. Yes, It is overhead you don’t need to fetch full background of a resource, It will also work if you get that object by just its direct parent.

Answer to Q#4 is:
To avoid (b) of Q#4 :

resources :hotels do
 resources :food_menus
# only: [] => We did not specified any route in it,
# So it will create no non-nested route for food_menu

resources :food_menus, only: [] do
 resources :categories

My Inspiration


I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with: ,
Posted in Uncategorized
One comment on “Nested Routes Rails precautions

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: