skip to content

In config/routes.rb, why do Rails developers keep nesting to one level, and what does shallow: true change for episodes nested under shows?

level: seniorimportance: should knowfreq 50%

answer

  1. one level deep rule of thumb
  2. collection nested, member flat
  3. index, new, create keep :show_id
  4. show, edit, update, destroy at /episodes/:id
  5. shallow: false opts a child out

basics

~20 s

Each nesting level adds a required id to every path and helper call, so Rails' guide advises one level. shallow: true keeps index, new and create under /shows/:show_id/episodes and moves show, edit, update and destroy to /episodes/:id.

solid answer

~30 s

Deep nesting makes every URL and helper carry ids the record already implies: a transcript under episodes under shows under networks needs `network_show_episode_transcript_path(network, show, episode, transcript)`, every template must load all four records, and the action must check that the ids agree. The routing guide's rule of thumb is one level. `shallow: true` is the built-in compromise: actions that need the parent to make sense stay nested (`index`, `new`, `create` on `/shows/:show_id/episodes`), while actions on one existing episode move to `/episodes/:id` with `episode_path(episode)` and `edit_episode_path(episode)`. Those member actions receive no `params[:show_id]`; the episode leads to its show.

code

ruby · 13 lines
ruby
# config/routes.rb
Rails.application.routes.draw do
  resources :shows, shallow: true do
    resources :episodes do
      resources :transcripts
    end
  end
end

# GET /shows/3/episodes          -> episodes#index    show_episodes_path(show)
# GET /episodes/42               -> episodes#show     episode_path(episode)
# GET /episodes/42/transcripts   -> transcripts#index episode_transcripts_path(episode)
# GET /transcripts/7             -> transcripts#show  transcript_path(transcript)

go deeper

for a junior

Know that nesting adds a required parent id to every path and helper, and that shallow: true moves the member routes back to /episodes/:id.

for a middle

Split the seven actions into collection and member groups and explain which group keeps the parent segment and why.

for a senior

Argue the cost of depth in templates, controllers and ownership checks, and choose plain, shallow or no nesting per resource rather than by habit.

for a principal

Set a routing convention for the codebase, such as one level with shallow member routes, so URL shape stays consistent as resources multiply.

## The cost of each nesting level A podcast host's ownership chain is real: a network has shows, a show has episodes, an episode has transcripts. Mirroring the whole chain in `config/routes.rb` is legal: ```ruby resources :networks do resources :shows do resources :episodes do resources :transcripts end end end ``` The price grows with every level: - **Paths** become `/networks/1/shows/3/episodes/42/transcripts/7`. - **Helper names** become `network_show_episode_transcript_path`, and every call needs all four records in the right order. - **Templates and controllers** must load every ancestor just to build a link, even when the page only shows one transcript. - **Consistency checks** multiply: the action should confirm that transcript 7 belongs to episode 42, which belongs to show 3, which belongs to network 1. - **Redundancy**: an episode id is already unique, so the outer ids add no information to a member URL. The routing guide's advice is a rule of thumb: nest resources only one level deep. ## shallow: true Shallow nesting keeps the context where it matters and drops it where it does not. The insight is that **collection** actions need the parent (which show's episode list? which show gets the new episode?), while **member** actions already have a unique id. ```ruby resources :shows do resources :episodes, shallow: true end ``` | Action | Path | Helper | |---|---|---| | `index` | `GET /shows/:show_id/episodes` | `show_episodes_path(show)` | | `new` | `GET /shows/:show_id/episodes/new` | `new_show_episode_path(show)` | | `create` | `POST /shows/:show_id/episodes` | `show_episodes_path(show)` | | `show` | `GET /episodes/:id` | `episode_path(episode)` | | `edit` | `GET /episodes/:id/edit` | `edit_episode_path(episode)` | | `update` | `PATCH / PUT /episodes/:id` | `episode_path(episode)` | | `destroy` | `DELETE /episodes/:id` | `episode_path(episode)` | It produces the same table as writing the two halves by hand: a nested `resources :episodes, only: [:index, :new, :create]` plus a top-level `resources :episodes, only: [:show, :edit, :update, :destroy]`. ## Where to put the option 1. **On the child**, as above, to make that one nested resource shallow. 2. **On the parent**, `resources :shows, shallow: true do ... end`, to make every resource nested inside it shallow, at any depth. With the four-level chain, each level then appears under only its direct parent: `/shows/:show_id/episodes`, `/episodes/:episode_id/transcripts`, `/transcripts/:id`. 3. **As a block**, `shallow do ... end`, to make every nesting inside it shallow. 4. **`shallow: false`** on a child cancels a parent's setting for that child. Custom member routes declared inside a shallow resource become shallow too, so `/episodes/:id/...` rather than the nested form. ## What the controller has to know The shallow member actions get `params[:id]` and **no** `params[:show_id]`. The controller therefore loads the episode by its id and reaches the show through the episode, while `index`, `new` and `create` still load the show from `params[:show_id]`. Two different lookups in one controller is the usual cost of shallow routes, normally handled by two small `before_action` methods. ## Mistakes interviewers listen for | Belief | What Rails actually does | |---|---| | `shallow: true` flattens every action | Only `show`, `edit`, `update` and `destroy` move; `index`, `new` and `create` stay nested | | Shallow member actions still get `params[:show_id]` | Their path is `/episodes/:id`, so only `:id` arrives | | Rails forbids nesting deeper than one level | Any depth is legal; one level is a guideline from the routing guide | | `shallow: true` on the parent covers only its first child | It applies to every resource nested inside it, at any depth | | Shallow nesting renames the controller | Requests still go to `EpisodesController` | ## Choosing between the shapes - **Plain nesting** fits when a child is only ever viewed inside its parent's context and the URL should say so. - **Shallow nesting** fits most owned collections: concise member URLs, parent context for lists and creation. - **No nesting** fits records with many possible parents or ones linked from everywhere, where a parent segment would be arbitrary. Interviewers use this question to see whether a candidate can name the cost of depth and reach for `shallow: true` before inventing a custom routing scheme.

  • After switching episodes to shallow: true, an episode page breaks. Why?
    Its URL moved from `/shows/:show_id/episodes/:id` to `/episodes/:id`, and the old helper calls `show_episode_path(show, episode)` and `edit_show_episode_path(show, episode)` are no longer defined. Templates must call `episode_path(episode)` and `edit_episode_path(episode)`, and the member actions can no longer read `params[:show_id]`; they reach the show through the episode.
  • One nested resource under a shallow parent must keep fully nested member URLs. How?
    Pass `shallow: false` on that child, for example `resources :credits, shallow: false` inside the shallow parent's block. It then draws its member routes under the parent's segment again, while its siblings stay shallow.

A library catalogue files new books under their shelf section, but once a book has a unique call number, nobody needs the floor, room and shelf to fetch it; the number alone is enough.

  • shallow: true moves index and create to the top level as well
  • Shallow member actions still receive params[:show_id]
  • Rails refuses to draw resources nested more than one level deep
  • shallow: true on the parent affects only its first nested resource
  • Shallow nesting changes the controller to Shows::EpisodesController