has_many and has_one RESTful Routing
Until the day comes that Rails’ routing can interrogate your domain model and properly translate the existing ActiveRecord associations into RESTful routes, we will have to settle for the same sugary-sweet declaration syntax. Instead of nesting resources in your routing configuration to imply an association, you can now directly specify the routing association with has_one and has_many:
map.resources :posts, :has_one => :author, :has_many => [:comments, :trackbacks] |
This gets you what you’re used to seeing as:
1 2 3 4 5 |
map.resources :posts do |posts| posts.resource :author posts.resources :comments posts.resources :trackbacks end |
You can always drop back to the nested form to specify more detailed options, but for vanilla routes this gets you an intuitive way to specify resource relationships that are directly reflected in your routing.
Auto Routing Name Prefixing
As part of the same update you no longer have to specify a named prefix for nested resources (to avoid conflicts with other named routes) – now named prefixes are assumed for you based on the resource nesting. For instance, this routing:
1 2 3 |
map.resources :posts do |posts| posts.resources :comments end |
now provides a post_comments_url(post_id) helper method. Previously, you had to specify the post_ part of the name via the name_prefix option for your route:
1 2 3 |
map.resources :posts do |posts| posts.resources :comments, :name_prefix => "post_" end |
Now that prefix is assumed for you (an assumption that could potentially break your routing and helper methods). If you don’t want the name_prefix assumed, you’ll need to explicitly set it to nil with :name_prefix => nil.
And don’t forget about the routing namespace update too
tags: ruby, rubyonrails

Awesome. Both of these changes are simple and intuitive.
How would I go more than 1 level deep?
/groups/[GROUP_ID]/posts/[POST_ID]/comments/[COMMENT_ID]
I would like to know the prent/child relationship of resources programatically, i.e, in:
map.resources :posts do |posts| posts.resources :comments end
Is there any way i could know that there is a PostsController and a CommentsController and that comments are children of posts? (ie inspecting some collection of ActionController::Base::Resource objects or something like that?) That would be fantastic for automating the scaffolding of controllers.
Thanks!