# How to consume a nested singular resource?

**URL:** <https://discuss.rubyonrails.org/t/how-to-consume-a-nested-singular-resource/35199>\
**Category:** rubyonrails-talk\
**Created:** [October 9, 2008, 12:08pm UTC](https://discuss.rubyonrails.org/t/how-to-consume-a-nested-singular-resource/35199 "2008-10-09T12:08:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [October 9, 2008, 12:08pm UTC](https://discuss.rubyonrails.org/t/how-to-consume-a-nested-singular-resource/35199/1 "2008-10-09T12:08:31Z")

</div>

Hi,

We have a RESTful server application which exposes the following routes using the following rules, but when trying to consume those resources from a client application the routes are not generated correctly. And we are not sure how to get around that.

The routes.rb specifies the following resources:

ActionController::Routing::Routes.draw do |map| &nbsp;&nbsp;map.resources :users do |user| &nbsp;&nbsp;&nbsp;&nbsp;user.resource :blog &nbsp;&nbsp;end

&nbsp;&nbsp;map.connect ':controller/:action/:id' &nbsp;&nbsp;map.connect ':controller/:action/:id.:format' end

Generated routes (only a subset):

POST /users/:user\_id/blog {:controller=\>"blogs", :action=\>"create"} POST /users/:user\_id/blog.:format {:controller=\>"blogs", :action=\>"create"} new\_user\_blog GET /users/:user\_id/blog/new {:controller=\>"blogs", :action=\>"new"} formatted\_new\_user\_blog GET /users/:user\_id/blog/new.:format {:controller=\>"blogs", :action=\>"new"} edit\_user\_blog GET /users/:user\_id/blog/edit {:controller=\>"blogs", :action=\>"edit"} formatted\_edit\_user\_blog GET /users/:user\_id/blog/edit.:format {:controller=\>"blogs", :action=\>"edit"} user\_blog GET /users/:user\_id/blog {:controller=\>"blogs", :action=\>"show"} formatted\_user\_blog GET /users/:user\_id/blog.:format {:controller=\>"blogs", :action=\>"show"} PUT /users/:user\_id/blog {:controller=\>"blogs", :action=\>"update"} PUT /users/:user\_id/blog.:format {:controller=\>"blogs", :action=\>"update"} DELETE /users/:user\_id/blog {:controller=\>"blogs", :action=\>"destroy"} DELETE /users/:user\_id/blog.:format {:controller=\>"blogs", :action=\>"destroy"}

The routes are exactly as we would like them. i.e. per user there is only one blog.

The problem starts when trying to consume these resources using active resource from a client application (ie another rails app to consume the REST api resources). Here is a description of the models:

class User \< ActiveResource::Base &nbsp;&nbsp;self.site = "[http://localhost:3000/&quot](http://localhost:3000/&quot); end

class Blog \< ActiveResource::Base &nbsp;&nbsp;self.site = "[http://localhost:3001/users/:user\_id/&quot](http://localhost:3001/users/:user_id/&quot); end

CRUD operations for the User model work fine. No problem there. But on inspection of the collection and element paths for the Blog model the following urls are generated:

Blog.collection\_path(:user\_id =\> 1) =\> "/users/1/blogs.xml"

Blog.element\_path(1, :user\_id =\> 1) =\> "/users/1/blogs/1.xml"

On trying to create a blog for a specific user:

b = Blog.new(:params =\> {:user\_id =\> 1})

> > b.save

ActiveResource::ResourceNotFound: Failed with 404 Not Found

the client application gets a 404 because the generated url is incorrect and the REST API application throws an ActionController::UnknownAction exception because the generated route is routed to the wrong controller and action, in this case:

/users/1/blogs.xml

gets routed to the users controller and tries to execute the 1 action. So, this cannot work, since the REST api cannot resolve these urls to routes since we have specified a singular resource i.e. "blog".

Can anyone help us with specifying how the consuming active resource models generate the correct urls for a singular nested resource? As we don't want to change the api specification as it is proper and concise.

Thanks in advance for your time.
