These instructions will step you through the process of upgrading a project from Rails 4 to Rails 5.
Before doing any of the following, ensure that you have Rails 5.1.2 installed (rails -v
). If not, run gem install rails -version=5.1.2
.
Let's call the existing Rails 4 project OP (Old Project), and the new Rails 5 project NP (New Project). The steps are as follows.
- The update-rails branch will be the primary branch for this update.
- Make a branch off of this branch.
- To make things a bit easier to keep track of, please use the following naming convention. All sub-branch names should begin with the prefix of
update-rails-
. For instance, if I'm updating AJAX Twitter, I'd name my branchupdate-rails-ajax-twitter
.
- To make things a bit easier to keep track of, please use the following naming convention. All sub-branch names should begin with the prefix of
- Once you finish up your work, please make a pull request where update-rails is the base and your branch is the compare.
- Add Louis, Hope, and Luke as reviewers of the pull request. Once two out of the three of us has reviewed your request, it will be merged into
update-rails
.
- Create a new Rails project with the same name as OP. Make sure it is created with Rails 5.
- Don't forget to initialize with Postgres!
- Gemfile:
- All uses of
shoulda-matchers
require a special branch to be specified in the gemfile:
gem 'shoulda-matchers', git: 'https://github.com/thoughtbot/shoulda-matchers.git', branch: 'rails-5'
- If your project uses authentication, comment
BCrypt
back in to the gemfile. - If your project uses authentication or relies on jQuery, add
gem 'jquery-rails'
. Otherwise, don't add it. - If you need to setup RSpec, do that from the beginning. Also install Factory Girl if that's needed.
- All uses of
- Generate an NP model for each OP model.
- If you have RSpecs to deal with, and if all is properly set up, running
rails g model
andrails g controller
should generate the relevant tests for you. - Your Gemfile's
:development
block will probably look like this:gem 'pry-rails' gem 'byebug' gem 'better_errors' gem 'binding_of_caller'
- Your Gemfile's
:development, :test
block will probably look like this if you have to deal with specs:gem 'shoulda-matchers', git: 'https://github.com/thoughtbot/shoulda-matchers.git', branch: 'rails-5' gem 'rails-controller-testing' gem 'rspec-rails', '~> 3.5' gem 'factory-girl-rails'
- If you have RSpecs to deal with, and if all is properly set up, running
- If you need to copy over any other migrations, be aware that Rails 4 migrations inherit from
ActiveRecord::Migration
, while Rails 5 migrations inherit fromActiveRecord::Migration[5.1]
. - Copy the contents of the OP models to the NP models. Note: Rails 4 migrations inherit from
ActiveRecord::Base
, while Rails 5 migrations inherit fromApplicationRecord
. - Generate an NP controller for each OP controller.
- Copy the contents of the OP controllers to the NP controllers. Don't forget
ApplicationController
. - Copy over all the views for OP to NP. You can just copy individual files over - no need to change any code.
- Copy over the contents, if any, of the
seed
file. - Copy over the content of the OP tests to the NP tests. Note, in Rails 5, controller tests require the
rails-controller-testing
gem, so make sure to add it to the gem file if there are any controller tests.gem 'rails-controller-testing'
- Copy over the
frontend
folder if there is one. - Copy over the
package.json
andwebpack.config.js
if they exist.
- Run the app, make sure it works
- Test very thoroughly. Log in and out, create stuff, run specs: whatever the app does, make sure it still works.
- Debugging tips:
- Note that
belongs_to
now validates the existence of the parent in Rails 5. - Remember to add the
rails-controller-testing
gem if there are any controller tests. - Remember
ActiveRecord::Migration
becomesActiveRecord::Migration[5.1]
. - Remember
ActiveRecord::Base
becomesApplicationRecord
.
- Note that
- There shouldn't be much to change here.
- If you had to do anything differently and that thing is mentioned in the README, make sure to update that part.
- If any code snippets look different in Rails 5 than they did in Rails 4, make sure to update them.
- In Rails 5 we can use
rails db:some_command
rather thanrake db:some_command
. This is the new standard.rake
shouldn't appear anywhere in the README once you are done.