# Symmetrical Routes for Nested Resources

**URL:** <https://www.ruby-forum.com/t/symmetrical-routes-for-nested-resources/142216>\
**Category:** Rails\
**Created:** [July 4, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/symmetrical-routes-for-nested-resources/142216 "2008-07-04T18:05:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SingleShot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@SingleShot](https://www.ruby-forum.com/u/SingleShot)\
**Post date:** [July 4, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/symmetrical-routes-for-nested-resources/142216/1 "2008-07-04T18:05:01Z")

</div>

I’m having trouble setting up nested resources is a certain way that I  
believe makes sense.

Given rules like these:

- an owner owns many books
- a book can be owned by many owners
- an owner can own multiple copies of the same book

I have a many-to-many relationship of Owners-to-Books with the “link”  
table (we’ll call it “Ownerships” since I can’t think of a nice name)  
having an attribute for tracking the number of copies of a book an  
owner owns.

I have no problem listing all books, or listing all owners:

- [http://127.0.0.1:3000/books.xml](http://127.0.0.1:3000/books.xml)
- [http://127.0.0.1:3000/owners.xml](http://127.0.0.1:3000/owners.xml)

What I can’t figure out is how to set up my routes/controllers to list  
all books owned by a particular owner, and all owners that own a  
particular book (i.e. what I call “symmetrical routes for nested  
resources”). I’d like the URLs to look something like this:

- [http://127.0.0.1:3000/books/3/owners.xml](http://127.0.0.1:3000/books/3/owners.xml) (lists all owners of book

1. 

- [http://127.0.0.1:3000/owners/7/books.xml](http://127.0.0.1:3000/owners/7/books.xml) (lists all books owned by  
owner 7)

What’s extra tricky is that I want the listings to also contain the  
number of copies from the “link” table. So, for example,  
[http://127.0.0.1:3000/owners/7/books.xml](http://127.0.0.1:3000/owners/7/books.xml) might return the equivalent  
of this as XML:

To Kill A MockingBird, 2  
The Zombie Survival Guide, 1  
War and Peace, 4

Please help this noob 🙂

Thanks,

Mike

---

<div class="post-metadata">

**Author:** ![SingleShot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@SingleShot](https://www.ruby-forum.com/u/SingleShot)\
**Post date:** [August 25, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/symmetrical-routes-for-nested-resources/142216/2 "2008-08-25T13:00:37Z")

</div>

I have a same problem 🙂

---

<div class="post-metadata">

**Author:** ![SingleShot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@SingleShot](https://www.ruby-forum.com/u/SingleShot)\
**Post date:** [August 25, 2008, 1:34pm UTC](https://www.ruby-forum.com/t/symmetrical-routes-for-nested-resources/142216/3 "2008-08-25T13:34:37Z")

</div>

Hi, you might want to consider using a join model when you have a many  
to  
many. Ryan B. covers this very nicely in his screencast that can be  
found here:

> **[\#47 Two Many-to-Many - RailsCasts](http://railscasts.com/episodes/47-two-many-to-many)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Good luck,

-Conrad
