[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 | # Gemfile |
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 | # Dockerfile |
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 | # Gemfile |
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 | # config/storage.yml |
(Object doesn’t support #ai)
It may be the XXX_print that cause this problem.
-
[amazing-print/amazing_print: Pretty print your Ruby objects with style – in full color and with proper indentation - https://github.com/amazing-print/amazing_print]
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 | # Gemfile |
References
[1] Ruby 3.0.0 Released - https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/