| name | rails-upgrade-4-to-5 |
| description | Upgrade a Ruby on Rails app from 4.x to 5.x — dual-boot via next_rails, ApplicationRecord introduction, ActionCable arrival, strong parameters becoming mandatory, params no longer hash-like, the version hop sequence, the Rails 5.1 system tests and `yarn` arrival, the Rails 5.2 ActiveStorage + credentials. Use when upgrading from Rails 4 to 5, the user mentions ApplicationRecord, strong parameters, params.permit, ActionCable, the params HashWithIndifferentAccess change, or asks how to leave Rails 4. |
Rails 4 → 5 Upgrade
Rails 4.2 → 5.0 is one of the biggest API breaks in the past decade. params stop being a Hash. Strong parameters become mandatory. ApplicationRecord arrives. Treat as a multi-month project for a non-trivial app.
The opinion
Dual-boot with next_rails. Hop 4.2 → 5.0 → 5.1 → 5.2. Ruby 2.2.2+ for Rails 5.0; Ruby 2.5+ for 5.2. Budget weeks-to-months for a non-trivial app.
The hop sequence
4.2 → 5.0 → 5.1 → 5.2
Core patterns
Pattern 1: 4.2 → 5.0 — the breaking changes
Required code changes:
class User < ActiveRecord::Base
end
class User < ApplicationRecord
end
bin/rails generate application_record
params change — biggest gotcha:
params[:user][:email]
params.permit(:foo, :bar)
User.create(params[:user])
params[:user][:email]
params.merge(...)
User.create(params[:user])
Audit every Model.create(params[:foo]) / update(params[:foo]) call. Add permit.
Other 5.0 changes:
- ActionCable arrives (no impact unless you use it).
belongs_to is required by default (optional: true to opt out).
- Test framework default switches;
Rails.test may need --clean runs.
head :ok after a respond_to block is no longer needed (slight behavior change).
Pattern 2: 5.0 → 5.1
- yarn arrives for managing JS packages.
- System tests (Capybara wrapper) ship.
form_for / form_tag start to be deprecated in favor of form_with.
secrets.yml partially deprecated (full deprecation in 5.2).
Pattern 3: 5.1 → 5.2
- Active Storage ships (file uploads).
- Encrypted credentials (
config/credentials.yml.enc) replace secrets.yml.
Date.current recommended over Date.today for time-zone safety.
EDITOR=vim bin/rails credentials:edit
Pattern 4: belongs_to required
class Post < ApplicationRecord
belongs_to :author
end
belongs_to :author, optional: true
Audit every belongs_to for optional: true cases.
Common mistakes to refuse
- Don't skip 5.0 (the Parameters change has to land cleanly).
- Don't allow
Model.create(params[:foo]) in 5.0+; permit everything.
- Don't migrate ActionCable + ApplicationRecord + secrets.yml in the same PR.
See also
rails-upgrade-3-to-4 — previous hop
rails-upgrade-5-to-6 — next hop
Sources