OmbuLabs Blog

The Lean Software Boutique

Upgrade Rails from 5.0 to 5.1

This article is part of our Upgrade Rails series. To see more of them, click here.

This article will cover the most important aspects that you need to know to get your Ruby on Rails application from version 5.0 to 5.1.

  1. Ruby version
  2. Gems
  3. Config files
  4. Application code
    1. ActiveRecord
    2. Controllers
  5. Testing
  6. Next steps

1. Ruby version

Like Rails 5.0, Rails 5.1 requires Ruby 2.2.2 or later.

2. Gems

  • Make sure the gems you use are compatible with Rails 5.1, you can check this using Ready4Rails. If a gem is missing on Ready4Rails, you'll need to manually check the Github page for the project to find out its status. In case you own the gem, you'll need to make sure it supports Rails 5.1 and if it doesn't, update it.

3. Config files

Rails includes the rails app:update task. You can use this task as a guideline as explained thoroughly in this post.

As an alternative, check out RailsDiff, which provides an overview of the changes in a basic Rails app between 5.0.x and 5.1.x (or any other source/target versions). Always target your upgrade to the latest patch version (e.g: 5.1.6 instead of 5.1.0).

Some assets configuration changes you'll have to do on your config/environments/{development, test, production}.rb files are:

Before:

config.serve_static_files = false
config.static_cache_control = "public, max-age=3600"

After:

config.public_file_server.enabled = false
config.public_file_server.headers = "public, max-age=3600"

4. Application code

4.1. ActiveRecord

  • The raise_in_transactional_callbacks option is now removed. It was already deprecated and covered in a previous upgrade.

  • Also removed was use_transactional_fixtures, which was replaced by use_transactional_tests.

  • ActiveRecord::Base#uniq was removed, it was deprecated in Rails 5.0 and has been replaced by #distinct. Check out https://github.com/rails/rails/pull/20198 for the discussion.

4.2. Controllers

  • Before Rails 5.1, conditions in filters could be invoked using strings. They now have to be symbols:

Before

  before_action :authenticate_user!, unless: 'has_project_guest_id'

After:

  before_action :authenticate_user!, unless: :has_project_guest_id
  • All *_filter methods are now called *_action:

These methods were actually already deprecated in Rails 5.0, and Rails 5.1 removes support for *_filter usage, so you should be using *_action.

Before:

skip_before_filter :authenticate_user!
before_filter :authenticate_user!
after_filter :do_something

After:

skip_before_action :authenticate_user!
before_action :authenticate_user!
after_action :do_something

5. Testing

  • Parameters in controller tests now need a params key:

Rails 5.0 had already deprecated this behavior, and Rails 5.1 drops support for passing parameters without using keyword arguments. This change is necessary even if you're using RSpec:

Before:

expect { post :create, params }.to change(Project, :count).by(1)

After:

expect { post :create, params: params }.to change(Project, :count).by(1)

6. Next steps

If you successfully followed all of these steps, you should now be running Rails 5.1! Do you have any other useful tips or recommendations? Share them with us in the comments section.

If you don't have the time to upgrade your Rails app, check out our Ruby on Rails upgrade service: FastRuby.io