Writing
Upgrading Rails: What Rails 5 Changed and How to Upgrade Today
Rails 5 introduced ApplicationRecord, Action Cable, API-only apps and required belongs_to, which most Rails apps still use. Here is what changed since, and how to upgrade one minor version at a time.
By Ka Lun Chan · Web development / Architecture
What did Rails 5 change, and how do you upgrade a Rails app today?
Rails 5 set the shape most Rails apps still have: ApplicationRecord, bin/rails for every task, parameters as objects, required belongs_to, Action Cable and API-only apps. Upgrading today means moving one minor version at a time, running bin/rails app:update, and switching on new defaults gradually through load_defaults.
I first wrote this as two posts on my old blog, one on what Rails 5 introduced and one with my upgrade checklist. This version merges them, keeps what held up, corrects a few mistakes and updates the parts the Rails world has moved past.
As a CTO I led a re-platform of a portfolio of publishing titles from WordPress to Rails on AWS, so I tend to read release notes for what an upgrade will break before what it adds.
| Change | In Rails 5 | Now |
|---|---|---|
| ApplicationRecord | New base class for models | Still the default in every new app |
| rails instead of rake | bin/rails runs Rake tasks | Still the way to run tasks |
| Parameters object | No longer a hash | to_h raises on unpermitted params; params.expect in Rails 8 |
| Required belongs_to | Missing parent fails validation | Default since load_defaults 5.0; opt out with optional: true |
| *_filter callbacks | Deprecated | Removed in 5.1; use before_action and friends |
| render nothing: true | Deprecated | Removed in 5.1; use head :ok |
| Turbolinks 5 | Rewrite with a new API | Replaced by Turbo (Hotwire) in Rails 7 |
| Webpacker | Optional in 5.1 | Retired; importmap-rails or jsbundling-rails |
What did Rails 5 add that still matters?
Action Cable, API-only apps, rendering outside controllers, the attributes API and or queries. All five are still part of Rails, and the first two changed how people build with it.
- Action Cable was one of the best things in Rails 5. It adds real-time communication over WebSockets with a persistent connection between server and client. Rails 8 adds Solid Cable, which relays those messages through the database instead of Redis.
- API-only apps come from
rails new my_api --api, which leaves out the view and asset layers. It’s the natural fit when a separate frontend owns the UI, as in React with a Rails backend. ActionController::Rendererrenders templates outside a controller action, which is handy for broadcasting HTML over Action Cable or from a background job.- The attributes API lets you override the type Active Record infers from a column, or add attributes that have no column at all.
Relation#orfinally let you write OR conditions without dropping into SQL strings.
# Attributes API: override the type Active Record infers from the column
class StoreListing < ApplicationRecord
attribute :price_in_cents, :integer
end
# or: combine two relations with OR
Order.where(status: "pending").or(Order.where(priority: "high"))What did Rails 5 change in existing apps?
Models gained a shared ApplicationRecord base class, rails replaced rake on the command line, parameters stopped behaving like a hash, belongs_to became required, and several old APIs were deprecated or moved into gems.
ApplicationRecord works like ApplicationController: one abstract class where shared model behavior lives. Upgrading meant creating the file and changing every model to inherit from it. The code in my original post had typos (Class Order in a file called order.b), so here it is corrected:
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
# app/models/order.rb (Rails 4)
class Order < ActiveRecord::Base
end
# app/models/order.rb (Rails 5 and later)
class Order < ApplicationRecord
belongs_to :customer
belongs_to :coupon, optional: true # allowed to be missing
endRequired belongs_to catches orphaned records early, but it fails validation on any association that was quietly allowed to be nil. Mark those optional: true rather than turning the setting off globally.
My original post said ActionController::Parameters became an object instead of a hash and that .to_h converts it back. The first half is right: it stopped inheriting from HashWithIndifferentAccess. The second half needs a correction. Current Rails raises ActionController::UnfilteredParameters if you call to_h on parameters you haven’t permitted, which is the point. Permit first, and in Rails 8 use params.expect.
The deprecations from that list are now removals. The *_filter callbacks and render nothing: true were deprecated in 5.0 and removed in 5.1. RecordTagHelper (content_tag_for, div_for) moved to the record_tag_helper gem, XML serialization to activemodel-serializers-xml, and respond_with with class-level respond_to to the responders gem.
class OrdersController < ApplicationController
before_action :set_order, only: :show # was before_filter
def create
@order = Order.create!(order_params)
head :created # was render nothing: true
end
private
def order_params
params.expect(order: [:customer_id, :total]) # Rails 8
# params.require(:order).permit(:customer_id, :total) on older versions
end
endWhat happened to Turbolinks and Webpacker?
Both are gone from new apps. Rails 7 made Hotwire (Turbo and Stimulus) the default in place of Turbolinks and Rails UJS, and replaced Webpacker with import maps by default or jsbundling-rails when you need a bundler.
When I wrote the original, Turbolinks 5 was the news: a rewrite with an incompatible API, with the old version renamed Turbolinks Classic. Rails 5.1 then added Yarn and optional Webpack through Webpacker. That direction didn’t last. The Webpacker README now says it has been retired and points to jsbundling-rails or importmap-rails. Rails 8 also replaced Sprockets with Propshaft as the default asset pipeline.
For an older app, this is usually the most work in the whole upgrade, and it can be done separately from the Rails version bump. Moving from Webpacker to jsbundling-rails with webpack keeps your build familiar. Moving to import maps removes Node from the build but may mean rewriting how you load JavaScript.
How do you upgrade a Rails app today?
Get good test coverage, upgrade Ruby as its own step, then move Rails one minor version at a time: latest patch of your current version, fix deprecations, next minor version, repeat. At each step run bin/rails app:update and adopt new defaults gradually.
The current release is Rails 8.1, which needs Ruby 3.2.0 or newer. Rails 5 needed Ruby 2.2.2 or newer, not 2.2.1 as I originally wrote. My old checklist said to bump the Gemfile and run bundle update rails. That still starts each step, but the official guide adds the parts that make it safe:
# Gemfile: move to the next minor version only
gem "rails", "~> 8.1.0"
bundle update rails
bin/rails app:update # review every diff it proposes
bin/rails test # or bundle exec rspec- Fix every deprecation warning before moving on. Going one minor version at a time is what gives you those warnings before the removal.
- Treat
bin/rails app:updateas a proposal. It offers to overwrite files such asconfig/application.rb, so diff each one instead of accepting all. - Leave
config.load_defaultsalone at first. The update task createsconfig/initializers/new_framework_defaults_8_1.rb(named for the target version). Enable its settings one at a time, possibly across several deploys, then delete the file and raiseload_defaults.
Staying current matters because of the maintenance policy. Each minor series gets bug fixes for one year and security fixes for two years after its first release, with a new feature release roughly every six months. An app that skips upgrades for a couple of years ends up on an unsupported version, which is how technical debt becomes a business problem.
Which gems need attention during an upgrade?
Every gem that hooks into Rails internals. Check each one’s changelog for the Rails version it supports, and expect renames and major version bumps along the way.
My Rails 5 list was exception_handler, cancancan, rspec-rails and factory_girl_rails. Two lessons from it still apply. rspec-rails ties its major versions to Rails versions (rspec-rails 8 supports Rails 7.2 to 8.1), so it moves with every upgrade. And factory_girl_rails is now factory_bot_rails. thoughtbot renamed it because the old name confused some developers and was offensive to others, so the rename touches your Gemfile and every FactoryGirl reference in your specs.
I keep a shorter list of the gems I use in most Rails projects, and fewer gems makes every upgrade cheaper.
Sources
Checked in November against the Upgrading Ruby on Rails guide, the Rails 5.0, 5.1 and 8.0 release notes, the configuring guide, the API documentation for attributes and parameters, rubyonrails.org and its maintenance policy and Rails 7 announcement, the Webpacker README, factory_bot’s note on its name and the rspec-rails README. The table and checklist are mine.
- Ruby on Rails Guides: Upgrading Ruby on Rails
- Ruby on Rails Guides: Rails 5.0 release notes
- Ruby on Rails Guides: Rails 5.1 release notes
- Ruby on Rails Guides: Rails 8.0 release notes
- Ruby on Rails Guides: Configuring Rails applications
- Rails API: ActiveRecord::Attributes
- Rails API: ActionController::Parameters
- Ruby on Rails: Maintenance policy
- Ruby on Rails: Rails 7, fulfilling a vision
- Webpacker README (retired)
- factory_bot: why the name changed
- rspec-rails README: supported Rails versions
Short answers
What did Rails 5 introduce?
Action Cable for WebSockets, API-only apps with rails new --api, ActionController::Renderer, the attributes API and Relation#or. It also added ApplicationRecord, ran Rake tasks through bin/rails, made parameters an object rather than a hash and made belongs_to required by default.
Should I upgrade Rails one version at a time?
Yes, one minor version at a time. The official upgrading guide recommends moving to the latest patch of your current version, fixing deprecations, then moving to the next minor version, because deprecation warnings appear before each removal.
What do rails app:update and new_framework_defaults do?
bin/rails app:update interactively updates configuration files for the new version, so review each diff. It also creates a new_framework_defaults initializer. Enable its settings gradually, then delete it and raise config.load_defaults to the new version.
What replaced Turbolinks and Webpacker?
Rails 7 made Hotwire, meaning Turbo and Stimulus, the default in place of Turbolinks and Rails UJS. Webpacker is retired; new apps use import maps by default, or jsbundling-rails when they need a bundler such as esbuild or webpack.
Start with the next minor version
Check which Rails version your app runs against the maintenance policy, then plan the next minor version bump with its deprecation warnings fixed first. If the app is several versions behind, map the steps before you touch the Gemfile.