How To Upgrade Rails 2 To Rails 3.0.0

  • Post published:May 25, 2015

In a project if there is a need to upgrade the rails 2 version to its latest version 3.0.0, one need to install 2.3.5 first, then install rails 2.3.18 and then finally install 3.0.0. Recommended way is to do step by step upgradation. In this way, you will be able to diagnose and resolve issues one by one instead of trying to resolve all at once.

Rails upgradation from 2 – 2.3.5 – 2.3.18 – 3.0.0

Firstly, you need to run the following commands mentioned below to upgrade the dependent gems to rails 2.3.18:

I. gem uninstall rails -v 2.3.5
II. gem install rails -v 2.3.18

Start your project by running ./script/server command. Is the project working fine?? You need to verify it first because if it is not working fine, then there is a need to resolve the dependency issues. After verification, you need to check the required updates for moving the application to rails 2.3.18. All the commands that one have to run step by step must be run in project’s root directory.

Now, you need to install ‘rails upgrade’ plugin by running the command script/plugin install git://github.com/rails/rails_upgrade.git. To check for required updates, run rake rails:upgrade:check command and save the output of command in a text file. You also need to take the backup of application’s crucial files by running rakerails:upgrade:backup command. To upgrade rails 2.3.18 to to rails 3.0.0, ruby version 1.8.7-p374 also needs to be upgraded to ruby 1.9.2-p320. For this, you need to follow the steps explained below:

  • To install ruby 1.9.x, run rbenv install 1.9.2-p320 command
  • To set ruby 1.9.x as directory local ruby, run rbenv local 1.9.2-p320 command
  • To apply changes in rbenv for current directory, run rbenv rehash command
  • Finally upgrade the dependent gem by running gem install rails -v 3.0.0 command

For rails version 3.0.0, you need to make structural changes in rails project. Run rails new. command in project root directory. This command will ask you for further permissions about different files, whether you want to override them or not. It is important to check if you have a backup of that file or made no changes to rails file so that you can override the file. Either you may have an option to create backup of that specific file manually and then override the actual file or do not override that file. When you are done with it, then you need to transfer code to new places which are required in rails. Now move config/environment.rb file content to config/application.rb. For instance, like the content below:

ActionMailer::Base.delivery_method = :smtp
      ActionMailer::Base.smtp_settings = {
         :address => "162.248.98.197",
         :port => 25,
         :domain => "xyz.xyz.org",
      }
}

Further you need to install all the required gems by mentioning them in Gemfile. To get this done, move gems from config/enviornment.rb to Gemfile at project root directory. For instance, If Gems code in config/enviornment.rb file is:

Rails::Initializer.run do |config|
    #...
    config.gem "acts_as_indexed", :version=>"0.7.3"
    #...
end
require "will_paginate"
require "mysql"
require 'acts_as_indexed'

It will be moved to Gemfile with these changes:

gem “will_paginate”
gem “mysql”
gem “acts_as_indexed”, “>=0.7.3”

Now, look for the line below in config/enviroment.rb and remove it:
config.load_paths += %W( #{Rails.root.to_s}/extras )

After removing line, paste config.autoload_paths += %W( #{Rails.root.to_s}/extras ) in config/application.rb file and also add the following line:
config.autoload_paths += %W(#{Rails.root}/lib)

Now look for the line XYZ::Application.initialize! in config/enviroment.rb and change it to Rails.application.initialize! If lib/source option is specified for gem in environment.rb, then that will be renamed to require. You need to remove session related content from config/enviornment.rb file:

ActionController::Base.session = { 
  :key=> '_xyz_session', 
  :secret => 'SECRET-CODE-HERE' 
  }

In rails 3, this code belongs to an initializer file (config/initializer/session_store.rb). If we look at that file, we’ll see the code for configuring the application’s session data:

Mis::Application.config.session_store
:cookie_store,
:key => '_[YOUR_APP_NAME]_session'

Now run bundle install command. If you still face gem dependency issue, then Google it and solve accordingly. In this case, rake 10.4.2 was not working so we downgraded it to rake 0.8.7. If you are using ftools for file uploading, you may need to change require ‘ftools’ to require ‘fileutils’ in file_system_backend.rb in plugins. To make it work in rails 3.0.0 and ruby 1.9.2-p320, “attachment_fu” plugin hacks to work by using its 3.4 version. This is how we make it work in our project. Now updating rails view elements to new (rails 3.0.0) syntax, for instance form/links elements etc:

  • For form_forf.error_messages” has been deprecated in rails 3, so we replaced it with “f.object.errors.full_messages
  • <% form_for %> and <% form_tag %> has been replaced with <%= form_for %> and <%= form_tag%> respectively. Note the ‘=’ sign in ruby tags

As will_paginate (3.0.3) is stable for rails 3.0.0, so we switched to that version by specifying in our Gemfile as gem “will_paginate”, ‘=3.0.3’

For fixing controllers, remove “filter_parameter_logging :password” line from application_controller.rb as this line has been moved to config/application.rb in rails 3 with this change:
config.filter_parameters += [:password]

Finally change find(:all) and first queries to where, as :conditions key has been depreciated in first two. In where clause, we removed conditions key and used the latest syntax to define conditions.