Search code examples
ruby-on-railsroutesurl-routingnested-formsnested-resources

Rails nested rources and routes and how to set them up in route.rb


I think I have a handle on nested routes (great url helpers, and access and much more) and nested resources in forms with accepts_nested_attributes_for but what do I use in routes as I see both:

resources :schools do
   resources :documents
end

and also

resources :schools :has_many => :documents
end

please can you tell me the difference between these.
Obviously has_many is for a one-to-many relationship. does it produce path helpers and require correct routing and for the do block what relationship does that imply, none? just path helpers (/schools/documents) and what if I want multiple resources (other than books, say documents) under schools, the first way I can add it into the do-end block but what about the second way, just two lines, one for each has_many?
Though I've read the guides and api's I don't quite get the difference/usage here and anyone that can provide a clear explanation of the distinction between the two (in the form 'a does x whereas b does y' would be great) would be much appreciated :)

Oh and of course how they relate to having the has_many in the model - so I guess these relationships can be in the model with has_many, the controller (mostly thru usage of paths) and in the view (through forms with nested attributes).


Solution

  • They both do the same thing, its up to you to choose which one

    I prefer the do block format as its easier to read

    btw with the has_many format you could do :has_many => [:docs, :otherthings] for multiple nested routes