# How to consume a nested singular resource?

**URL:** <https://www.ruby-forum.com/t/how-to-consume-a-nested-singular-resource/150524>\
**Category:** Rails\
**Created:** [October 9, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/how-to-consume-a-nested-singular-resource/150524 "2008-10-09T14:08:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [October 9, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/how-to-consume-a-nested-singular-resource/150524/1 "2008-10-09T14: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|  
map.resources :users do |user|  
user.resource :blog  
end

map.connect ‘:controller/:action/:id’  
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  
self.site = “[http://localhost:3000/](http://localhost:3000/)”  
end

class Blog \< ActiveResource::Base  
self.site = “[http://localhost:3001/users/:user\_id/](http://localhost:3001/users/:user_id/)”  
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.
