Last week I got a chance to see Andre Lewis speak at the Silicon Valley Ruby Meetup about RESTful resources. While REST is not a new technology, it was only recently implemented in Rails with the release of 1.2. Anyway, while listening to Andre’s presentation I became intrigued with the idea of using REST in one of my applications. The presentation was quite informative and described a lot of the basics of getting started with RESTful resources. He also talked about nested resources which sparked a question which nobody in the room was sure about: How do you deal with resources that are nested within multiple other resources? Is it possible to do it dynamically?
One of the suggested resources to learn more about REST was Geoffrey Grosenbach’s Peepcode. For those not in the know, Peepcode is a site where Geoffrey releases monthly screencasts covering various topics in Ruby on Rails for the cost of $9 an episode. Considering the length of the episodes (75-85 minutes) its a great deal. Anyway, not to long ago an episode dealing with REST was published. After Andre’s talk I popped over to Peepcode and picked it up. It covered much of what Andre discussed, only in more detail. It also explained that HABTM relationships do not play well with REST, so that means my discrepancy app for work is not a good candidate to refactor for REST. However my recent dj mix project just might be. While the screencast was informative, and encouraged me to purchase other episodes, unfortunately my question posed at the meetup was still not answered.
My quest to have my question answered next took me to the Rails-talk Google group, formerly the Rails mailing list. So far I’ve received one response:
You could have a filter in the books_controller to pick out any author_id, publisher_id, etc. and scope the Book.find(…) according to these params. That should be simple and DRY.
If this is the way to go, then it might do to put together some meta-programming to create some dynamic finder methods so if the resource is nested in another resource later, no additional work would be required to make it functional.
One resource I came across that was very helpful in showing how to use REST in a real world applicaiton was the source code for beast. As I explored the source, I got a much clearer picture of how to properly put together my controllers and models.
REST, depending on what you’re working on, can drastically simplify your code; however it isn’t always appropriate to implement REST in your application. A careful examination of your design will let you know whether simplifying your interface is both possible and preferable.
Well, that’s it for now.
Sphere: Related Content