Rails gets more mature

Posted by Luke Francl
on Friday, May 02

Rails 2.1 is right around the corner. I’ve been following the new features in Edge Rails and eagerly looking forward to this release. Rails 2.1 includes a number of features that will make developers’ lives easier. Here’s a few of my favorites.

Necessary directories created if they don’t exist

Neither Mercurial nor Git track empty directories. This is a pain with Rails, because you have to create a file in the log directory to make sure it gets created when you check out code, otherwise Rails won’t start. This is no longer needed, because Rails will create necessary directories if they don’t exist.

Time zone support

Time zones are a huge pain in any application, in any language because they are just plain confusing. But ya gotta do it. In Rails, the solution used to be using the TzTime and TzInfoTimeZone plugins. Rails 2.1 adds support for tracking Time objects with their time zone. This is going to make everyones’ lives a lot easier. Check out Geoff Buesing’s in-depth tutorial.

Partial updates and “dirty” tracking

Two features that I knew and loved in our home-brew ORM from my former life as a Java developer have made it into Rails.

With dirty objects you can know if you need to persist an object, and which attributes have changed, and what an attribute’s previous value was. This will be great for user messages and validations!

In Rails 2.1, ActiveRecord can update only the attributes which have changed. This can (sometimes) put your objects into an inconsistent state, but partial updates improve performance, especially when you have big TEXT or BLOB attributes that haven’t changed. Use optimistic locking to prevent users from stomping on each others’ changes.

Timestamped migrations

With all this distributed SCM going on, the classic problem of messed up migrations gets way worse. I talked about solutions to this in my talk at acts_as_conference, one of which was timestamped migrations. Timestamped migrations allow interleaved migrations. As long as those migrations don’t conflict with each other, they can be applied in any order. This has been added to Rails. Nice!

Better gem dependency and unpacking

I am a big fan of the vendor everything approach to gems because I got burned way too many times by missing gems.

But it doesn’t always work (for example, gems which must be natively compiled are a problem), and you have to install one of the various vendor everything plugins—and everyone seems to use a different one. In Rails 2.1, gem unpacking is built in with rake gems:unpack GEM=gemname. (more info)

And for those gems that don’t work, you can list them as a dependency. Your app will fail to start if the gem is not installed. Fail early, fail often!

Text helpers usable outside the view

You can now use helpers without including them into your class. Hurray!

Comments

Leave a response

  1. Andrew VitMay 02, 2008 @ 05:02 PM

    My favorite one is named_scope. I think this will change a lot about how we define and use common conditions for find.

    e.g.

    named_scope :published, :conditions => {...}

    then, anywhere you need it:

    Post.published.find :all

  2. makoto kuwataMay 02, 2008 @ 07:53 PM

    Good article. Can I translate your work into Japanese?

  3. Stephen CelisMay 03, 2008 @ 07:55 AM

    Andrew,

    Actually, you can just call:

    Item.published (find :all is called on the dangling scope, though you can use find or count if you want to retrieve different information)

    Even better, you can layer them:

    Item.published.featured

  4. Luke FranclMay 03, 2008 @ 11:00 AM

    makoto kuwata, please do! Send me a link if you do.

    Cheers, Luke

  5. Luke FranclMay 03, 2008 @ 11:04 AM

    I need to look into named_scope. It has the potential to really clean up some code I’m working on, but I need to be able to access the current user…class level variable, here we come?

  6. makoto kuwataMay 03, 2008 @ 06:06 PM

    Luke, thank you for your permission. Here is a translation into Japanese: http://d.hatena.ne.jp/kwatch/20080503/1209855784 Thank you for your great job!

  7. elhumanoMay 06, 2008 @ 03:55 PM

    So no compisite primary key yet… Maybe some Datamaper support on Rails would be a great future…