Choosing your application's version of Rails

When Learning Rails released, Rails was on version 2.1. Version 2.2 appeared a little while later, and 2.3 just came out a couple of weeks ago. Progress in software is always ahead of progress on previously printed pages, so there will be a few complications.

I'm sorting out what's actually changed in Rails - and fortunately the answer so far is "not that much covered in the book" - but I've realized that the book didn't explain something fairly important: how to change which version of Rails an application is running under. In reading the book, this matters mostly if you download the sample code, which was built under Rails 2.1.

If the first version of Rails you've ever installed on your computer is version 2.2, Rails won't find the code it needs to run the application. There are two ways to fix that. The first, as Mike Smith pointed out in a review in the UKUUG Newsletter, is to specify an installation with Rails version 2.1 - gem install -v=2.1.0 rails. That will let you work with all of these code samples in the environment where they were originally created. (And I'll be adding that as errata to the book!)

RailsConf, co-produced by Ruby Central, Inc. and O'Reilly Media, Inc., is the largest official conference dedicated to everything Rails. Through keynotes, sessions, tutorials, panels, and events, RailsConf is an interactive meeting ground for the most innovative and successful Rails experts and companies. RailsConf 2009 is happening May 4-7 in Las Vegas, Nevada. Register today!

On the other hand, if you want to work with newer versions of Rails, perhaps because you're migrating your own application to use newer features, or simply because you prefer updating to stay current, you'll need to change one setting in a file and then run a rake task. First, open the config/environment.rb file. Near the top, you'll see something like:

# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.1.0' unless defined? RAILS_GEM_VERSION

Change the 2.1.0 to 2.2.2, 2.3.2, or whatever is most appropriate. After you've done that, go to the command line for your application and run:

rake rails:update

Rake will clean up your config files and do anything else necessary. For 2.3, it's especially critical, because application.rb is renamed to application_controller.rb, and your application won't run at all if that isn't fixed.

Rake won't, of course, do anything to make sure that the rest of your code still works, and odds are good that you'll find some breakage along the way. This should, however, get you started!

And if you find it's too much breakage, you can just freeze Rails at a particular version with the rake rails:freeze:gems task.