Using Multiple Versions Of Rails

It’s quite easy to have multiple versions of rails installed as gems when working with ruby on rails. Quite frequently, you will be working on a project that uses an older version of rails than one on your machine, and all you have to do to get the correct version is:

gem install -v 1.2.3 rails --include-dependencies

Multiple versions of rails can exist independently on your machine without much of a problem. Since your individual rails projects store the version number they expect, and your projects don’t tend to depend on each other, you can have hundreds of different rails versions installed and never notice. Mostly.

If, however, you wish to start a NEW project, when you run

rails projectname

It will use the most recent version of rails. But what if, for some reason, you have rails 2.0.1 and rails 1.2.6 on your machine, and wish to generate a rails 1.2.6 application? This came up because I’m trying to teach a friend rails, and the book he has is for rails 1.2, meaning that if he uses 2.0 all of the scaffolding code (as well as other bits) from the book are incorrect. At the same time, the application he and I are working on together uses rails 2.0, so he needs both versions on his machine.

This solution works in linux (haven’t tried windows):

rails _1.2.6_ projectname

I didn’t see any documentation about this anywhere. In fact, I found it by cracking open the actual ‘rails’ script in /usr/bin and noticing that it did a regex match for a parameter that started and ended with underscores, then interpreted that as the version.

In any case, this allows you to happily generate a new rails app using an old version of rails. Nothing quite like discovering an undocumented feature by reading the source code.


5 Comments

  1. John Trupiano:

    Thanks, this helped me. FYI, I was juggling 2.0.2 and 2.1.1, and this advice still worked. Prior to finding this post, I did in fact open up the executable myself, however I failed to notice the _’s in the regexp.

    -John

  2. Brad:

    Thanks, answered my question.

  3. DL:

    Thanks for posting this. You’ve just saved me a ton of time and helped me with ensuring backwards compatibility of some code I’m modifying.

  4. M:

    Thanks! This was just what I needed. (I’m developing a 2.3 app and learning from the same book)

  5. This Blog Needs No Name | April | 2009:

    [...] somewhere. I google for “multiple versions of rails” and find a brief blog post about Using Multiple Versions Of Rails that gives me this useful piece of information: your individual rails projects store the version [...]

Leave a comment