This article is part of our Upgrade Rails series. To see more of them, click here.
- Ruby version
- Config files (config/)
- Application code
- Next steps
1. Ruby version
This Ruby upgrade shouldn't generate any problems. However, if you run into this exception in your test suite:
cannot load such file -- test/unit/assertions (Load Error)
then you'll need to add:
- It's recommended that you check your
Gemfileagainst Ready4Rails to ensure all your gems are compatible with Rails 5. As of the release of this blog post, there are only a few gems which don't support Rails 5 yet. This is more of a problem when you're upgrading early on.
If any of the gems are 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 works on Rails 5 or update it.
3. Config files
As an alternative, check out RailsDiff, which provides an overview of the changes in a basic Rails app between 4.2.x and 5.0.x (or any other source/target versions).
4. Application code
- ActiveRecord models will now inherit from ApplicationRecord by default instead
of ActiveRecord::Base. You should create an
application_record.rb file under
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end
And then update all of your models to inherit from
ActiveRecord::Base. The only class that inherits from
belongs_toassociations are now required by default. This means that if the association doesn't exist for a model when saving it, an error will be triggered. You can turn this feature off for an association by using
belongs_to :user, optional: true
- If you're not already using strong parameters, and still rely on
protected_attributes, you should migrate your application to strong
parameters before attempting to upgrade to Rails 5.0.
no longer supported on Rails 5.0.
protected_attributes, you should migrate your application to strong parameters before attempting to upgrade to Rails 5.0.
protected_attributesis no longer supported on Rails 5.0.
A few months ago we worked on a project to attempt to automate the strong parameters migration process, and this resulted in the gem RailsUpgrader. It's in a beta state, but you can try using it if you have too many models, or at least as a guide for a WIP branch.
There are still efforts being made to keep
protected_attributes alive though,
gem. I would strongly recommend against using it since its support is limited,
and it won't work in future Rails versions.
- Parameters now behave differently, they no longer inherit from
HashWithIndifferentAccess. Some methods (e.g.:
except) you may be now calling on
paramswill no longer work. You will need to
permitthe parameters, call
to_h, and only then you'll be able to run the
Hashmethods you need on them.
For more information: https://github.com/rails/rails/pull/20868
- One of the most common methods in controller tests,
assigns, has been extracted from Rails and moved to the
rails-controller-testinggem. If you wish to continue using it (and
assert_template), you'll need to add it to your
- Instead of using
ActionDispatch::Http::UploadedFileto test uploads, you'll need to update those tests to use
6. Next steps
If you successfully followed all of these steps, you should now be running Rails 5.0! Do you have any other useful tips or recommendations? Share them with us in the comments section.