Use a specific Rails version during project creation
If you do any amount of Rails programming, at some point you end up with various versions of Rails installed on your system. Generally this is fine as Rails keeps what version should be used in it’s environment.rb file.
However when you are creating a new project, the system rails command will use the most recent gem installed.
Also usually fine unless you know the latest version breaks something you want to use. Andy had mentioned to me before that it was possible to specify the version Rails should use on the command line when generating the initial project. However I forgot what it was. Lately I have been on a code reading kick so I decided to start at the start and see if I could remember what he had told me.
Well it didn’t take long. The 3rd line of actual code in the system `rails` command is:
if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
You can see, if you specify:
rails _2.0.2_ project_name
And that particular Rails version is correct and installed, it will use that version. Thanks to Andy for pointing that out.
This trick works with a lot of gem-installed commands.
For example, if you have Capistrano 1.4.1 and 2.x installed, the Cap2 version will be what’s run when you type `cap`. But you can use the old version by typing `cap _1.4.1_`. Pretty nice if you’ve got old apps still using Capistrano 1!