# Learning REST: Multiple routes to the same resource?

**URL:** <https://www.ruby-forum.com/t/learning-rest-multiple-routes-to-the-same-resource/118924>\
**Category:** Rails\
**Created:** [November 1, 2007, 11:58am UTC](https://www.ruby-forum.com/t/learning-rest-multiple-routes-to-the-same-resource/118924 "2007-11-01T11:58:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [November 1, 2007, 11:58am UTC](https://www.ruby-forum.com/t/learning-rest-multiple-routes-to-the-same-resource/118924/1 "2007-11-01T11:58:24Z")

</div>

I just started looking at REST, and am working on a simple app made up  
from Authors and Books with an Author has\_many :books relationship.

I’m thinking the following resources make sense:

/books  
books/book\_id  
/books/book\_id/author  
/authors  
/authors/author\_id  
/authors/author\_id/books

I.e. listing books, listing authors, looking up a book or author,  
listing books for a certain author, and accessing the author of some  
specific book.

Is this reasonable, or am I already doing something wrong? I’m  
guessing that /books/book\_id/author may be a little over the top.

As for routes, I now have the following:

map.resources :books do |book|  
book.resource :author  
end  
map.resources :authors do |author|  
author.resources :books  
end

According to rake routes, there’s a fair amount of controller+action  
duplication here.  
Is this ok, should I be looking at parameters to figure out what to do?

Here’s the index action from my books controller right now. This one  
isn’t too messy, but I’m sure other actions will turn out worse…?

# GET /books[.xml]

# GET /authors/author\_id/books[.xml]

def index  
if params[:author\_id].nil?  
@books = Book.find(:all)  
else  
@books = Author.find(params[:author\_id]).books  
end

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @books }
end

```

end

TBH I’m a little lost, and should probably be looking for books or  
something. Recommendations appreciated.

Thank you,  
Isak

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [November 1, 2007, 12:21pm UTC](https://www.ruby-forum.com/t/learning-rest-multiple-routes-to-the-same-resource/118924/2 "2007-11-01T12:21:11Z")

</div>

On Nov 1, 2007 9:57 PM, Isak H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> TBH I’m a little lost, and should probably be looking for books or  
> something. Recommendations appreciated.

try this one, lots of ruby examples  
[http://www.oreilly.com/catalog/9780596529260/](http://www.oreilly.com/catalog/9780596529260/)

and this

[http://peepcode.com/products/restful-rails](http://peepcode.com/products/restful-rails)

–  
George Bray, Canberra, Australia
