Posts Tagged ‘habtm’

learning to REST

January 25th, 2007

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.

Tags: , , , ,
Posted in general | Comments (2)

has_many :through issue cont.

January 7th, 2007

After getting some sleep and then re-reading my earlier post, I realized that I was as not as clear as I’d like to be about my problem with has_many :through.

Just about every example I found of how to use has_many :through showed how to use it to read data and most were explaining how to use it for polymorphic associations. What I needed was to know how to create the data for a non-polymorphic relationship. This highlights a problem with the Rails framework, and the one thing I miss from PHP. Documentation. When looking at the API, I frequently find areas that have no commentary at all, and other areas where the commentary is incomplete. A lot of people have hit brick walls because of this, more than one google for help on a specific problem brought me across a forum post or a blog post describing frustration at the lack of information in the API. Read the rest of this entry »

Tags: , , , ,
Posted in general | Comments (0)

has_many :through antics

January 6th, 2007

Update: I mistakenly reported that AWDwR did not present the solution to my problem. It did on page 339. Please see my later post for specifics.

So in my effort to get back into the swing of things with Ruby and Rails I decided to write an app to manage my dj mixes. The app would allow for entering a mix with a tracklist and download sets. It will also provide an itunes optomized rss feed for podcasting and eventually the ability to digest Traktor playlists and dynamically generate cue sheets.

Initially I worked on getting the mix model and controller working which was pretty easy. After that I created the tracklist controller with the tracklist and track models. Here I struggled. the tracklist model was a join model. I was going to use habtm but I needed to store the track order (and later track start times for cue sheets). So, like a good little ruby nuby I set up a has_many :through relationship between mixes and tracks through tracklists.

Now I thought that I could create a@mix instance and pass it to the tracklist controller, then use @mix.tracks.create(params[:track]) to create the track and and the association record in the tracklist model. Unfortunately it wouldn’t work. Read the rest of this entry »

Tags: , , , , ,
Posted in general | Comments (0)