Topic: Need help with consuming a nested singleton resource!


I have a REST api with a nested singleton resource. The routes look like this:

  map.resources :users do |user|
    user.resource :blog

Then I have a client rails app to test consuming the REST api. The models look like this:

class User < ActiveResource::Base = "http://localhost:3000/"

class Blog < ActiveResource::Base = "http://localhost:3000/users/:user_id/"

So, the problem is the type of urls (routes) which the client active resource models generate. They don't match the routes for the singleton resource of the REST api and therfore do not work.

The routes generated by the REST api are as follows (only a subset of course):

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 problem is that when trying to call normal actions on the blog active resource from the client, the server throws an exception because the route cannot be matched, the url which the blog model is generating is:

For example:

b = Blog.find(:first, :params => {:user_id => 1}) # works fine
b.reload # receives a 404 since the server REST api cannot match the following route


Which is obviously wrong since the routes do not have an :id parameter because the resource is singular.

Does anyone have any experience, help or links in building active resource models which properly consume a nested singular resource. I really hope someone can help!