[Ruby on Rails (RoR)] Upgrade to Ruby 3 and Rails 6.1

Ruby 3 and Rails 6.1

Ruby 3, whose goal is performance, concurrency, and Typing. Especially about performance, Matz stated “Ruby3 will be 3 times faster than Ruby2” a.k.a. Ruby 3x3.

Rails 6.1 has been released and wow does it have a lot of great stuff! It have implemented improvements to multiple databases, adding support for destroying associations in jobs instead of in-memory, turning errors into objects, and so much more.

Ruby 3.0.0

Ruby 3.0.0 covers those goals by:

  • Performance

    • MJIT
  • Concurrency

    • Ractor

    • Fiber Scheduler

  • Typing (Static Analysis)

    • RBS

    • TypeProf

Visit Ruby 3.0.0 Released - https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/ to learn more about Ruby 3.

Rails 6.1

Rails 6.1 have some of the new functionality:

  • Multi-DB Improvements

  • Strict Loading Associations

  • Delegated Types

  • Destroy Associations Async

  • Error Objects

  • Active Storage Improvements

  • Disallowed Deprecation Support

  • Performance Improvements and Bug Fixes!

  • The classic Autoloader is Deprecated

Visit Rails 6.1: Horizontal Sharding, Multi-DB Improvements, Strict Loading, Destroy Associations in Background, Error Objects, and more! | Riding Rails - https://weblog.rubyonrails.org/2020/12/9/Rails-6-1-0-release/ to learn more about Rails 6.1.0.

Prerequisites

  • Prepare test cases and do a regression testing after version upgrade.

  • Back up code and data, you can roll back to the previous version when the upgrade fails

Upgrade

Upgrade rvm.

1
$ rvm get head

Install Ruby 3.

1
$ rvm install 3.0.0

Edit the file Gemfile.

1
2
3
4
5
6
7
# Gemfile

ruby '3.0.0'

gem 'rails', '~> 6.1.0'

gem 'bundler', '2.2.8'

Then, run bundle update.

1
$ bundle update

The upgrade process will be completed after solving some gem version dependency compatibility issues.

Remember to run the regression testing to discover and solve compatibility issues as early as possible.

Docker, Docker Compose, Kubernetes (K8S)

Remember to change ruby version to 3.0.0 if you use Dockerfile.

1
2
3
4
5
# Dockerfile

ARG RUBY_VERSION=3.0.0

FROM ruby:${RUBY_VERSION}

Removals, Deprecations or Notable changes, etc

Rails 6.1 ActiveRecord deprecates update_attributes and update_attributes! methods

update_attributes and update_attributes! is deprecated and will be removed from Rails 6.1 (please, use update! instead)

As suggested, we need to,

  • Replace update_attributes with update

  • Replace update_attributes! with update!

Visit Ruby on Rails 6.1 Release Notes — Ruby on Rails Guides - https://guides.rubyonrails.org/6_1_release_notes.html to leanrn more Removals, Deprecations or Notable changes, etc.

FAQs

There may have some gems about version dependency compatibility.

Bundler: You must use Bundler 2 or greater with this lockfile

The issue was with gemfile.lock because our local bundle version and project bundle version was not matching!

You must upgrade gem ‘bundler’ to 2.2.8+.

1
$ gem update --system

Exception: ArgumentError: wrong number of arguments (given 4, expected 3) on marginalia-1.8.0

You must upgrade gem ‘marginalia’ to 1.10.0+.

1
2
3
4
5
# Gemfile

# Attach comments to ActiveRecord's SQL queries
# https://github.com/basecamp/marginalia
gem 'marginalia', '1.10.0'

RuntimeError: Couldn’t find Active Storage configuration in config/storage.yml

We need to create the file config/storage.yml manually, if the file does not exist.

1
2
3
4
5
6
7
8
9
10
11
12
# config/storage.yml

# Active Storage Overview — Ruby on Rails Guides
# https://edgeguides.rubyonrails.org/active_storage_overview.html

local:
service: Disk
root: <%= Rails.root.join("storage") %>

test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

(Object doesn’t support #ai)

It may be the XXX_print that cause this problem.

You cant try to disable them by comment the relevant codes.

NoMethodError (undefined method `encode’ for URI:Module)

gem ‘carrierwave-aliyun’ old version use `URI#encode’ which have been removed from Ruby 3.

You must upgrade gem ‘carrierwave-aliyun’ to 1.2.3+.

1
2
3
4
5
# Gemfile

# huacnlee/carrierwave-aliyun: 阿里云 OSS Ruby 上传组件,基于 Carrierwave
# https://github.com/huacnlee/carrierwave-aliyun
gem 'carrierwave-aliyun', '1.2.3'

Merge branch ‘encode_url’ · huacnlee/carrierwave-aliyun@36c03af - https://github.com/huacnlee/carrierwave-aliyun/commit/36c03af0762eb30fe4baac690441c30ed8651520

References

[1] Ruby 3.0.0 Released - https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/

[2] Rails 6.1: Horizontal Sharding, Multi-DB Improvements, Strict Loading, Destroy Associations in Background, Error Objects, and more! | Riding Rails - https://weblog.rubyonrails.org/2020/12/9/Rails-6-1-0-release/

[3] Active Storage Overview — Ruby on Rails Guides - https://edgeguides.rubyonrails.org/active_storage_overview.html

[4] ruby on rails - Bundler: You must use Bundler 2 or greater with this lockfile - Stack Overflow - https://stackoverflow.com/questions/53231667/bundler-you-must-use-bundler-2-or-greater-with-this-lockfile

[5] Rails 6.1 ActiveRecord deprecates update_attributes methods - https://rubyinrails.com/2019/04/09/rails-6-1-activerecord-deprecates-update-attributes-methods/

[6] Ruby on Rails 6.1 Release Notes — Ruby on Rails Guides - https://guides.rubyonrails.org/6_1_release_notes.html