Guide to Upgrade Ruby on Rails
By: Balamurugan R
Tags: Gem Audit, Programming, Software Development, Rails Upgrade, Ruby on Rails

This article gives you the general ideas and challenges faced while upgrading the application to the newer version of Ruby On Rails.

Things to consider before we start the upgrade

As Rails guide suggests, it’s good to have test automation before we start the Ruby On Rails upgrade.

Developers handy with the product can upgrade pretty much faster than a newbie.

Upgrade one minor version at a time to make good use of the deprecation warnings. Rails version numbers are in the form Major. Minor.Patch

Do a Gem audit before you start the upgrade.

Check all the major gems that provide support for the targeted version of Rails. If not find the alternate gem.

Check the gems that are well maintained.

Updating one gem at a time worked for us.

Here is an idea for a gem audit table. Columns are Gem Name, Gem Description, Current version of Gem, Current Rails Version, Target Rails Version, Supports Target Rails Version(yes/no), Target Gem version, Any alternative Gems.

Benchmarking application performance metrics is important. These help you compare the results before and after the migration and help us identify with any post-production performance issues

“Target Rails Version” column should consider security vulnerabilities aspects also NOT only functional compatibility

Ruby Versions

Rails generally stays close to the latest released Ruby version when it’s released.

Rails 6 requires Ruby 2.5.0 or newer.

Rails 5 requires Ruby 2.2.2 or newer.

Rails 4 prefers Ruby 2.0 and requires 1.9.3 or newer.

Rails 3.2.x is the last branch to support Ruby 1.8.7.

Rails 3 and above require Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially. You should upgrade as early as possible.

Let's go through the upgrade process

The idea here is to upgrade to the nearest compatible version fix the issue and then again move to the next available version.

Update the Ruby version in .ruby-version file and reload the directory.

Update the Rails gem version in gemfile and run bundle update rails.

Update task, Rails provides the app:update command (rake rails:update on 4.2 and earlier). After updating the Rails version in the Gemfile, run this command. This will help you to create new files and change old files in an interactive session.

Then upgrade other gems used in the application. If you have issues with upgrading the gem, then go to the Gem documentation and find the upgrade process if it exists.

Move to the latest patch version after your current version.

Fix tests and deprecated features. Will explain this briefly below.

Move to the latest patch version of the next minor version.

Repeat this process until you reach your target Rails version. Each time you move versions, you will need to change the Rails version number in the Gemfile (and possibly other gem versions) and run bundle update. Then run the Update task mentioned in the 3rd step to update configuration files, then run your tests.

Fix tests and deprecated features

Here is where most of the Application code changes will happen. Rails guides provide the detailed documentation of what needs to change as part of upgrading the Ruby On Rails. Here is the list of topics of Rails guide.

Release notes have new features implemented. It's nice to check and use these features as part of the rails upgrade.

Few Challenges faced during Upgrade

Here are some challenges faced during the upgrade.

Upgrading the gems needs a lot of effort, because few gems were not supported and few gems names have been changed, etc. For not supported gems we need to find alternative gems and Remove old gem implementation and add new gem implementation. Or else fork the old gem from GitHub and update as per your needs.

Resolve changes for ActiveRecord query and ApplicationRecord abstract class and ActiveRecord validation.

Resolve function name changes Before_filter and After_filter becomes before_action and after_action,ActionMailer deliver becomes deliver_now changes.

Resolve changes for Strong parameter implementation.

These are some problems we faced when upgrading Ruby On Rails.I hope this helps. Please let us know if there is any feedback.