Here at the WTG, we love to be on the bleeding edge. Which is why we installed the Rails 3.0.0.beta gem on our development server. But of course, Rails 3 has a way to go, and we might not be upgrading our production apps for a while. So in the meantime, we’re working in both worlds.
A problem comes up if we try to put together a new Rails 2 app. In order to build the app, you need to run the executable for Rails 2, but when we upgraded to Rails 3, running “rails” from the command line results in a shiny Rails 3 app, with no path to execute the older versions easily.
Rubygems provides a nice syntax for dealing with gem versions, in most cases. You can specify a particular version of the gem that you want to use on the commandline as the first argument to the command and in underscores:
rails _2.3.5_ myapp
But between Rails 2 and Rails 3, the rails bin moved from the Rails gem to the Railties gem (Railties is a dependency of Rails). So the above syntax doesn’t work anymore.
Fortunately, the executable that you run when typing /usr/bin/rails is a short ruby script that delegates all the functionality to some scripts located within the gem (see the source here). All we have to do is modify that file to be a little more intelligent. I’m not accounting for Rails 1, just identifying calls requests that look like they want the Rails 2 executable.
Just replace your /usr/bin/rails (or wherever you keep your executable) with the following:
#!/usr/bin/ruby1.8 # # This file was generated by RubyGems, then edited for Rails 2 # backwards compatibility. # # The application 'railties' is installed as part of a gem, and # this file is here to facilitate running it. # require 'rubygems' version = ">= 0" rails_gem = "railties" if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then version = $1 ARGV.shift end if version =~ /^2.*$/ rails_gem = "rails" end gem rails_gem, version load Gem.bin_path(rails_gem, 'rails', version)
Now you should get:
$ rails _2.3.5_ --version # => Rails 2.3.5 $ rails --version # => Rails 3.0.0.beta
Now, for convenience, we might just want to have a Rails 2 executable. Put the following /usr/bin/rails2, and set it to be globally executable.
#!/usr/bin/ruby1.8 # # This file should be run to run the 'rails' command from the # rails 2 gem. # See http://webtech.union.rpi.edu/blog/2010/02/24/rails-2-and-rails-3/ require 'rubygems' version = "< 3.0.0.beta" if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then version = $1 ARGV.shift end gem 'rails', version load Gem.bin_path('rails', 'rails', version)
Just call “rails2″ to use the latest version of rails 2 available, or specify a particular rails 2 version. You can use this without having the above modification to ‘rails’ or have them both for maximum convenience.
$ rails2 --version # => Rails 2.3.5 $ rails2 _2.3.4_ --version # => Rails 2.3.4 # Works, but same effect as runnning rails 2.3.4 with the 'rails' script we wrote above. $ rails2 _3.0.0.beta_ --version # Doesn't work. Just use the 'rails' command.
This should all continue to remain working as future rails 3 gems come out, as long as the above files don’t get overwritten (you may have to re-paste the code if a future gem overwrites that, so keep this link around). It would be really nice to see this sort of functionality handled automatically. In the meantime, though, the above should keep you running comfortably.
All the above code is available here: http://gist.github.com/313946, and the originally generated /usr/bin/rails for Rails 3 is here. Comments welcome.

Have you tried RVM (http://rvm.beginrescueend.com/) and its gemsets? It is pretty nice.
[...] Edition and gem path: Updating the Path everywhere on Ubuntu see also post on Stack Overflow Rails2 and Rails3: So Happy Together Rails 3: Let ActiveRecord Manage Your Translations Be pragmatic with your time [...]
On OS X Snow Leopard I was getting the following error:
-bash: /usr/bin/rails2: /usr/bin/ruby1.8: bad interpreter: No such file or directory
I fixed it by changing the first line to what the original script was using:
#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
So before doing this, make you check the first line in your original /usr/bin/ruby script to see where Ruby is actually installed for your system
[...] version you are using rails2 -v #should bring out 2.3.9, or the rails2 version you are using I used this guide to construct the rails2 executable.Related Posts:Deploying Rails App With Heroku on Ubuntu 10.10 [...]