Skip to content

Something on Rails

April 20, 2006

I’m afraid I finally have to own up to it. Like a lemming I’ve been drawn to the latest shiny object that is Ruby on Rails. I couldn’t help it. I tried to look away but there it was, taunting me: try me, try me! I feel so weak.

I’ve been interested in Ruby for a while but so far I’ve never used it for anything very large. Just bits and pieces. With rails I thought I’d develop something of reasonable size to give myself a good opportunity to learn it properly and create something real from it to see how well it works for a real project. I choose to write a relatively complete issue tracking system. I’m still working on it but I’m far enough through it now that I feel I have a reasonable basis for evaluating how well rails works in practice.

Lets start with Active Record. As someone who has done a fair bit of Hibernate I can’t help but immediately compare the two. You would probably have to say that hibernate is a much more full-on O-R mapping system. You get the impression there are a lot of moving parts behind Hibernate. Active Record on the other hand is a lot closer to the database. It is a little like it is doing the minimum amount of work to get the rows mapped into an object. Both schemes have their advantages of course.

Hibernate’s caching stands out to me as being better. This can be a pretty big help when trying to make your program performant. Hibernate is also has a slight edge when it comes to the flexibility of mappings it can perform. Honestly though if you’re connecting to a legacy schema I’m not sure an O-R mapper is necessarily a good idea anyway.

The downside to Hibernate is that you can shoot yourself in the foot more easily. It tends to be harder to diagnose what went wrong as well. I never felt this with AR however. There is nothing really tricky going on for most operations so most problems are skin deep. Typically you end up having to switch back to writing SQL anyway when more complicated queries are required. I actually think this is probably not such a bad thing. Although HQL lets me do some pretty complex things all in the object world I don’t actually see it as much of a problem having to drop back to SQL with AR.

I initially found it really bizzare when I had to switch to sql then bring back a bunch of fields that actually didn’t belong to object I was mapping to but in practice it works really well and avoids having to suck down a bunch of fields from related tables that you don’t necessarily need.

Another nice part of Active Record is the minimum amount of work you need to do to map records:

Here’s an example:

class Issue < ActiveRecord::Base
end

That’s it! Well okay not really. There’s extra stuff we can add in such as the relationships to other tables:

class Issue < ActiveRecord::Base
  has_many :comments
end

I don’t think you really need to be able to read ruby to understand what that is trying to express. Of course we’re relying on a few conventions here. Things get more verbose when we need to deviate from those conventions but not massively so.

One other bizare part of active record is that it supports validation directly in the model. This runs counter intuive to the expectations I’ve built up over the years with frameworks I’ve used in Java. Surely validation is a separate concern that belongs elsewhere! Think about it for a while however. Many validations fit very nicely with the model. One thing I’ve been increasingly noticing since working with Rails is the shear number of places in a project I need to modify do make just one small change in my Java projects. Code that changes together should stay together.

Another thing that rubs against the grain is that methods that I would usually split out into a separate DAO layer are placed together with the object in rails. To put it another way, query methods are generally placed into the domain object as static methods. Normally this would signal big warnings with me but I think the dynamic nature of Ruby makes this a little easier to deal with. I’m still not sure I like the idea of this but so far I haven’t had it actually trip me up in any meaningful way and it is certainly nice not having to jump around so much as I’d normally be doing with Java.

The other major part of rails is action pack which is the MVC side of things. Action pack is probably nothing we haven’t really seen before but it does it’s job well and is pretty simple to use and understand. Mapping URL’s to controllers and actions is simple and works well. As with AR much of the goodness in the MVC side is a result of Ruby itself.

What I love most about developing under rails has to be the fact that I can make a change, press refresh on my browser and immediately see the result. The compile/redeploy/test cycle in Java begins to really feel painful after using rails for a while.

Debugging consists of putting the breakpoint statement at the point you want your program to stop. It sounds primative but as you get full access to the the interactive ruby environement it actually works pretty well. I’ve also got a full blown debugger in my ruby IDE (archnoruby) but as yet I’ve had little reason to use it.

RJS templates
[2] are one thing that I really like about AP. It makes certain things in Javascript much easier to deal with. Prototype itself is also very nice but one thing that bugs me about many of these Javascript libraries is that the size of these libraries gets pretty big. I know most people are using broadband these days but there are still plenty of modem users out there.

As yet I haven’t played with any of the web services support yet so I can’t really comment on that but from what I’ve been of the documentation it looks reasonably straightforward. I found the action mailer support is pretty simple to use but it really isn’t doing anything particularly exciting.

Overall programming stuff in rails is a lot of fun. I’ll refrain from any statements regarding whether it is more productive or not. There are just too many things not directly related to programming that can take your time during any major development project that it actually probably does not matter nearly as much as many think it does. I’ll just say that if you’re after an enjoyable language to program webapps in then rails is a joy to program.

Advertisements

From → Ruby

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: