1

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).

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497

2 Answers2

1

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

Diego Salazar
  • 46
  • 2
  • 5
0

I think the has_many syntax was something added to Rails 2 as a shorthand for those that didn't like the block syntax. You can see a blog post about it here. I just tried it and it seems that Rails 3 ignores the has_many option. So the output for me was:

resources :schools do
   resources :documents
end

created the routes:

    school_documents GET    /schools/:school_id/documents(.:format)          {:action=>"index", :controller=>"documents"}
                     POST   /schools/:school_id/documents(.:format)          {:action=>"create", :controller=>"documents"}
 new_school_document GET    /schools/:school_id/documents/new(.:format)      {:action=>"new", :controller=>"documents"}
edit_school_document GET    /schools/:school_id/documents/:id/edit(.:format) {:action=>"edit", :controller=>"documents"}
     school_document GET    /schools/:school_id/documents/:id(.:format)      {:action=>"show", :controller=>"documents"}
                     PUT    /schools/:school_id/documents/:id(.:format)      {:action=>"update", :controller=>"documents"}
                     DELETE /schools/:school_id/documents/:id(.:format)      {:action=>"destroy", :controller=>"documents"}
             schools GET    /schools(.:format)                               {:action=>"index", :controller=>"schools"}
                     POST   /schools(.:format)                               {:action=>"create", :controller=>"schools"}
          new_school GET    /schools/new(.:format)                           {:action=>"new", :controller=>"schools"}
         edit_school GET    /schools/:id/edit(.:format)                      {:action=>"edit", :controller=>"schools"}
              school GET    /schools/:id(.:format)                           {:action=>"show", :controller=>"schools"}
                     PUT    /schools/:id(.:format)                           {:action=>"update", :controller=>"schools"}
                     DELETE /schools/:id(.:format)                           {:action=>"destroy", :controller=>"schools"}

while

resources :schools :has_many => :documents

created the routes:

    schools GET    /schools(.:format)          {:action=>"index", :controller=>"schools"}
            POST   /schools(.:format)          {:action=>"create", :controller=>"schools"}
 new_school GET    /schools/new(.:format)      {:action=>"new", :controller=>"schools"}
edit_school GET    /schools/:id/edit(.:format) {:action=>"edit", :controller=>"schools"}
     school GET    /schools/:id(.:format)      {:action=>"show", :controller=>"schools"}
            PUT    /schools/:id(.:format)      {:action=>"update", :controller=>"schools"}
            DELETE /schools/:id(.:format)      {:action=>"destroy", :controller=>"schools"}

I think the real answer to your question is that those are/were supposed to do the same thing, just with different syntax.

mikewilliamson
  • 24,303
  • 17
  • 59
  • 90