Upgrade Rails 2 application to Rails 3

Prabu D

3 min read

Last month, I spent most of time upgrading a beautiful application from Rails 2 to Rails.
It was such a wonderful experience and I just felt to share it with everyone.

rails3 The key tools are rails_upgrade plugin and rvm.

As a first step, lets upgrade the ruby itself using rvm

Update Ruby and Install Rails 3
Use RVM to install latest version of Ruby 1.9.3 and the install Rails 3.
Run rvm update to get latest headers.
[source language=”ruby”]
$> rvm get head
rvm reloaded …!
Once RVM has updated it will automatically reload itself otherwise we need to do that by running below command.
[source language=”ruby”]
$> rvm reload
Now we can use RVM to install Ruby 1.9.3.
[source language=”ruby”]
$> rvm install 1.9.3
This command will download, compile and install the latest version of Ruby 1.9.3.
To check installed ruby version
[source language=”ruby”]
$> ruby -v
ruby 1.9.3p327 (2012-11-10 revision 37606) [i686-linux]
$> rvm –default use 1.9.3
Now lets install rails 3
[source language=”ruby”]
$> gem install rails –pre
Note that since we used a rvm-installed version of Ruby we don’t need to use sudo to install gems.
Now lets move to next step.

To upgrade a Rails 2.3 application to Rails 3.2.

  1. Create a separate branch if the applications use Git for source control.
    [source language=”ruby”]
    $> git checkout -b rails3
  2. rails_upgrade ( https://github.com/rails/rails_upgrade ) – It is useful to see what changes we need to make.
    Useful command :
    [source language=”ruby”]
    # Check your app for required upgrades
    rake rails:upgrade:check
    # Backup your likely modified files that might be overwritten by the generator
    rake rails:upgrade:backup
    # Generate a new route file
    rake rails:upgrade:routes
    # Generate a Gemfile from your config.gem directives
    rake rails:upgrade:gems
    # Generate code for a new config/application.rb from your environment.rb
    rake rails:upgrade:configuration
    To install rails_upgrade plugin we first need to swap back to our Rails 2 environment.
    [source language=”ruby”]
    $ rvm system
    $rails2_app> script/plugin install git://github.com/rails/rails_upgrade.git
    Now we can analysis by using below command
    [source language=”ruby”]
    $rails2_app> rake rails:upgrade:check
    Before starting to upgrade, run the command
    [source language=”ruby”]
    $rails2_app> rake rails:upgrade:backup.
    This will back up some of the key files that are likely to be overwritten during the upgrade.
  3. We start the upgrade by creating a new Rails 3 application in the application’s
    directory. In order to do that we need to swap back to our Ruby 1.9.3 environment.
    [source language=”ruby”]
    $ rvm 1.9.3
    $rails2_app> rails new .
    Now create a new application in the same directory as an existing one, we’ll be asked if we want to overwrite some of the existing ones. If we’re not sure if a file should be overwritten we can use the d option to see the differences between the existing file and the one that will be overwritten.
    The rails2_app application has now been upgraded to Rails 3.2 but we still have to go through the files that we backed up and move over any code that is specific to our application. Each backed-up file will be in its original directory with a .rails2 extension
  4. Finally, if the file environment.rb file configures the application’s gems move those gems into Gemfile. The syntax has changed slightly and so the code need to be changed to look like this:
    [source language=”ruby”]
    gem ‘libv8’, ‘~> 3.3.10’
    gem ‘execjs’
    gem ‘therubyracer’, ‘0.10.2’
    # Gems used only for assets and not required
    # in production environments by default.
    group :assets do
    gem ‘sass-rails’, ‘~> 3.2.3’
    gem ‘coffee-rails’, ‘~> 3.2.1’
    gem ‘uglifier’, ‘>= 1.0.3’
    now we need to run bundle install to make sure that all of the required gems are installed.
  5. Try to run the rails application
    $rails2_app> rails s

Gem changes for rails 3.2.X :
1. Line cache
Problem : An error occurred while installing linecache (0.46), and Bundler cannot continue. Make sure that `gem install linecache -v ‘0.46’` succeeds before bundling.

Solution : To install latest linecache (manual)
[source language=”ruby”]
curl -OL http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
gem install linecache19-0.5.13.gem
2. Javascript runtime
Problem : autodetect’: Could not find a JavaScript runtime.
Solution : Add the below gems in Gemfile
[source language=”ruby”]
gem ‘libv8’, ‘~> 3.3.10’
gem ‘execjs’
gem ‘therubyracer’, ‘0.10.2’
3. therubyracer
Problem : An error occurred while installing therubyracer (0.11.0), and Bundler cannot continue.
Solution : Manual installation for libv8.
[source language=”ruby”]#> gem install libv8 -v=3.3.10[/source]
4. ruby-debug
Problem : linecache can’t handle 1.9.x yet. undefined symbol: ruby_current_thread
Solution : Modify the following gems in Gemfile
[source language=”ruby”]gem ‘linecache19’, ‘0.5.13’
gem ‘ruby-debug19’, :require => ‘ruby-debug’
gem ‘ruby-debug-base19x’, ‘0.11.30.pre10′[/source]
Problem : config/initializers/new_rails_defaults.rb:14: undefined method `generate_best_match=’ for ActionDispatch::Routing:Module (NoMethodError)
Solution : the comments in the file new_rails_defaults.rb indicate that it should be deleted
when Rails 3 is released. To fix remove the file from initializers/.
Problem : uninitialized constant ActionController::Dispatcher in config/initializers/omniauth.rb file.
Solution : change the line instead of “ActionController::Dispatcher.middleware.use OmniAuth::Builder do” to “Rails.application.config.middleware.use OmniAuth::Builder do”
Problem : uninitialized constant RAILS_ROOT (NameError)
Solution : Use Rails.root instead of RAILS_ROOT constant.
8. Application controller
Problem : “Expected /…/app/controllers/application.rb to define Application.”
Solution : remove or rename app/controller/applicaiton.rb into application_controller.rb
9. request uri
Problem : undefined method `request_uri’
Solution : replace the line in the file lib/login_system.rb
[source language=”ruby”]
def store_location
session[:return_to] = request.request_uri
by :
[source language=”ruby”]
def store_location
session[:return_to] = request.url
10. Library
Problem : In Rails 3 /lib modules are not loaded automatically.
Solution : added the below lines in the config/application.rb file.
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir[“#{config.root}/lib/**/”]
11. Use mysql2
Problem : Please install the mysql adapter: `gem install activerecord-mysql-adapter` (can’t activate mysql2)
Solution : Install the sql client package.
#> sudo apt-get install libmysqlclient-dev then in Gemfile file added gem ‘mysql2’.
12. Routing
Problem : This problem can sometimes be caused by disabling or refusing to accept cookies. redirect problem in routing.
Solution : Change the routes syntax based on rails3 update.
Hope you all liked this blog
Some useful links from railscasts

Related posts:

One Reply to “Upgrade Rails 2 application to Rails 3”

Leave a Reply

Your email address will not be published. Required fields are marked *