# Advanced rest interface tips

**URL:** <https://www.ruby-forum.com/t/advanced-rest-interface-tips/112761>\
**Category:** Rails\
**Created:** [August 22, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/advanced-rest-interface-tips/112761 "2007-08-22T23:07:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 22, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/advanced-rest-interface-tips/112761/1 "2007-08-22T23:07:14Z")

</div>

All the tutorials I see on doing a rest interface with rails only show  
how to do it with resources 1 level deep, i.e., they don’t show a good  
way to go about doing a rest interface for nested type rest  
interfaces. For example:

> **[service.com is for sale | www.brandforce.com](https://www.brandforce.com/domain/service.com/)**
>
> This premium domain name is available for purchase!

If I was doing this with rails would I have to use 2 different  
controllers: users\_controller & companies\_controller?

Are there any good guides for this?

Thanks

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 23, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/advanced-rest-interface-tips/112761/2 "2007-08-23T12:43:15Z")

</div>

Probably, and you can use nested routes like this:

map.resources :users do |users|  
users.resources :companies  
end

aurélien

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 23, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/advanced-rest-interface-tips/112761/3 "2007-08-23T15:20:38Z")

</div>

Yes, I would use two controllers. The users\_controller would look like  
a normal RESTful controller.  
The companies\_controller requires a little more work.  
I tend to do most of it with before\_filters

class CompaniesController \< ApplicationController

before\_filter :find\_user  
before\_filter :find\_companies, :only =\> :index  
before\_filter :find\_company, :only =\> [:show,:update,:edit,:destroy]

protected

def find\_user  
@user = User.find\_by\_id(params[:user\_id])  
end

def find\_companies  
@companies = @user.companies  
end

def find\_company  
@company = @user.companies.find\_by\_id(params[:id])  
end

public

def index  
…

For the new/create actions, you should use:  
@company = @user.companies.build(params[:company])

Which makes the new company belong to the @user and (I think) updates  
any counter cache you might be using.  
Your company\_path now takes two arguements, @user and @company; the  
companies\_path takes @user.

Those methods are the bare minimum, and in a real application, I would  
probably have something to redirect to a 404 if a user or company  
wasn’t found.

In your routes, you will need a declaration as aurélien said.

Depending on the nature of the application, you might want to  
substitute the @user in the build statement for something representing  
the currently logged in user. Something similar could be done in a  
filter method so that the searches for the update, edit, destroy  
actions only find companies belonging to the current user.

Hope this helps,  
Jonathan
