Search code examples
ruby-on-rails-3routesnested-resources

Rails 3 routing: Avoiding Deep Nesting


Today I realised I'd gotten a little carried away with nested resources:

resources :organisations do
  resources :studies do
    resources :settings
  end
end

The Rails guidelines (and my own thoughts) suggest that you shouldn't nest more than 1 level deep, so I refactored to this:

resources :organisations do
  resources :studies
end
resources :studies do
  resources :settings
end

Does anyone know a cleaner / more concise way to declare the above routes? Google gave me a lot of Rails 2-specific stuff.

Many thanks!


Solution

  • You pretty much got it figured out and on the right track. It really depends on your domain. Just looking at your routes, I would ponder on what Settings does. Maybe a namespace somewhere to handle settings would suffice, maybe not. Really depends on what you are trying to do.

    However, as far as nesting goes. It's looking fine.

    PS. You can also refer to this guide for routing in Rails 3.0.X.