Routing error in nested resources

I am using nested resources as follows:
  map.resources :topics do |topics|
    topics.resources :items do |items|
      items.resources :attachments

When I generate a RESTful path for new item in the console:
app.new_topic_item_path(2) then I get correct path as

However, for the new attachment I am getting error:
app.new_topic_item_attachment_path(22) gives -
ActionController::RoutingError: new_topic_item_attachment_url failed to
generate from {:action=>"new", :controller=>"attachments",
:topic_id=>22}, expected: {:controller=>"attachments", :action=>"new"},
diff: {:topic_id=>22}

        from (eval):17:in `new_topic_item_attachment_path'
        from (irb):5.

- - - - -

Why is it failing and how to fix this?
And, why is it generating '{:action=>"new", :controller=>"attachments",
:topic_id=>22}' ? Shouldn't it pass item_id rather than topic_id?

Any clues?

Hi Carlos!

Because of the nesting. For example, I recently hacked up a version of
Beast which is nested similarly to yours: Forums with many Topics with
many Posts. Creating a new post has the following resource path: /
Even though the topic_id in my case is globally unique, it still needs
the forum_id for that path.

You don't have to do that, though. You could leave them as non-nested
and then just use associations, i.e. Topics has_many Items

I like nested resources conceptually but they do make url generation a
little confusing.

Just remember to use: "rake routes" liberally so that you always know
what routes are available and how to map them.