Recent Posts by SingleShot

Subscribe to Recent Posts by SingleShot 2 posts found

Jul 7, 2008
Avatar SingleShot 2 posts

Topic: General Ruby/Rails Discussion / Symmetrical Routes for Nested Resources

Wow. I can’t imagine a better response to a software development question. It’s precisely what I needed, and it worked like a charm. Thanks!

 
Jul 6, 2008
Avatar SingleShot 2 posts

Topic: General Ruby/Rails Discussion / Symmetrical Routes for Nested Resources

I’m having trouble setting up nested resources in a particular way that makes sense to me.

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: 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:

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 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