home

my rant on Ruby on Rails

I’ve been waiting to give my $0.02 on Ruby on Rails in general. I’d like to think I’m playing it safe. When you’re a techno-junkie like me, you love staying on top of the latest techno-memes, gadgets, buzzwords, and hype.

The Ruby on Rails framework, despite being sooo lastweek.com, still qualifies as a “hott” new technology.

To qualify my rant, let me just say that I read (cover-to-cover) Agile Web Development with Rails. I even dove into the Ruby book (the pickaxe book as it is called by those in the know) a bit too. I started attending the local Ruby On Rails user group, and have setup the environment, toyed with it myself a bit, and started building a small little app.

Now, not to sound like I’m a know-it-all (heaven forbid this shameless self-promotional site should come off like that), but it appears that Rails has an answer for a lot of the things I’ve been pulling my hair out over in developing web apps. It turns out that enforcing a framework opens up the doors, rather than constraining the overall output. It turns out that favoring convention over configuration lets you do all kinds of fun stuff quickly. It turns out that using a pure object-oriented language for web applications makes inheritance and grabbing methods and overriding their default behavior fun and useful (and not just makes for a great looking UML chart across the conference room wall and a hauty-tauty design document with big words like polymorphism).

Just for instance, a long while back, it took me a few days (honestly: weeks) to really grok the importance (and actually implement) a real MVC (Model View Controller) pattern to my PHP development. It doesn’t take very long to realize that mixing SQL logic and tossing it into PHP variables and evaluating them in-line on a page is just Not Right — even though it is so very fun and easy when you (think you) know what you’re doing. So, putting my foot down and implementing a form of templating (and being good about not putting business logic in the templates/view) through the use of something like Smarty is good and takes care of separating the view — with the added benefit of modularity, caching, and a nice little sub-language to do neat tricks (truncating, formatting, html_escaping) — I was OK with that.

However, I’m still searching for a DB abstraction layer that’s as elegant and simple as Rail’s ActiveRecord. Yes, the Rails framework simplifies DB interaction, but only where it should. This is a common argument among developers that insist that since they know how to do something, that it makes no sense for a framework to handle it for them. My argument is that, just because you know how to write a document using paper and pencil (or vi or notepad), and can go through and format it and spell check it yourself — you don’t. You use a word-processor like Word (or Open-Office). Just because you can hack html by hand doesn’t mean you should. When you can (and want it look decent) you gotta whip out something like FrontPage or Dreamweaver.

To quote David Heinemeier Hansson (the creator of the Rails framework), who puts it quite eloquently:

Ever since programmers started to layer object-oriented systems on top of relational databases, they’ve struggled with the question of how deep to run the abstraction. Some object-relational mappers seek to eradicate the use of SQL entirely, striving for object oriented purity by forcing all queries through another OO layer.

Active Record does not. It was built upon the notion that SQL is neither dirty nor bad, just verbose in the trivial cases. The focus is on removing the need to deal with the verbosity in those trivial cases but keeping the expressiveness around for hard queries – the type SQL was created to deal with elegantly.

Therefore, you shouldn’t feel guilty when you use find_by_sql() to handle either performance bottlenecks or hard queries. Start out using the object-oriented interface for productivity and pleasure, and the dip beneath the surface for a close-to-the-metal experience when you need to.

Therein lies the beauty. Yes, it’s those trivial cases where you just what that one row or that one value, and you’ve gotta write that select * from blah where …. blah blah blah.

I’m really digging Rails right now. I know, I’ve jumped on the bandwagon, but I’m happy to be aboard.

Leave a Reply

Type the word "seebq" here: