How to consume a nested singular resource?


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

map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’

Generated routes (only a subset):

POST /users/:user_id/blog {:controller=>“blogs”,
POST /users/:user_id/blog.:format {:controller=>“blogs”,
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”,
PUT /users/:user_id/blog.:format {:controller=>“blogs”,
DELETE /users/:user_id/blog {:controller=>“blogs”,
DELETE /users/:user_id/blog.:format {:controller=>“blogs”,

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 = “http://localhost:3000/

class Blog < ActiveResource::Base = “http://localhost:3001/users/:user_id/

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 = => {:user_id => 1})
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:


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.