How to upgrade from rails 4.2.6 to rails 5.0

Even I am trying to upgrade my rails version to rails 5

This is a checklist to follow from here

  1. Change ruby version to 2.2.2

  2. Create application_record.rb in app/models/ and add:

    # app/models/application_record.rb
    class ApplicationRecord < ActiveRecord::Base
      self.abstract_class = true
    end
    

    and make sure that all your models inherit from it.

    # app/models/user.rb
    class User < ApplicationRecord
    end
    
  3. Create application_job.rb in app/jobs/ and add:

    # app/jobs/application_job.rb
    class ApplicationJob < ActiveJob::Base
    end
    
  4. Change rails version in Gemfile

    gem 'rails', '5.0.0'
    
  5. Replace debugger with byebug.

    Debugger is not supported by Ruby 2.2 which is required by Rails 5.

    gem 'byebug'
    
  6. bundle the gems

    bundle install
    

    bundle install will throw errors or warnings for some gems change their version to the required version by rails 5


I just went through this a few days ago, and this was my (successful) process:

First, make sure your app runs on Ruby 2.2+ before upgrading.

Then update Rails to the latest 4.x version (4.2.7 right now), and run your test suite (or fully exercise your app) watching the log for Deprecation warnings.

Fix those deprecations if you find any.

Carefully read the 5.0 release notes and make note of things that might affect your app.

Check your Gemfile with Ready4Rails, which should help tell you what gems you have that may not be ready for Rails 5 yet. [Note: Ready4Rails.net appears to have been taken down as of 1/13/2020]

You'll probably need to visit the home repositories for some of your gems to see if there is a branch that might have support, but isn't quite released yet. You may want to pin your Gemfile for these gems to a pre-release version or fork (even if you don't adopt right away, this can help you get things running for when the final version of those gems are released).

It may also help to reduce your dependencies in general. For instance, you might have a bunch of gems in your development block, that aren't updated yet. You can probably live without these for awhile.

Some people will advise you to remove version numbers in your Gemfile and bundle update. I would recommend against that for anything but very small apps. Changing too many things at once can make it very hard to track down issues.

Change the version of Rails listed in your Gemfile like this:

gem 'rails', '= 5.0.0'

and run bundle update rails.

This will likely fail and give you a list of version number comparisons that bundler couldn't resolve. Look for any dependencies that ultimately require rails to be less than 5, and see if those can be updated.

Change your version of that gem, undo the change to the version for rails. bundle update that gem, run your suite again (or exercise parts of your app that use that gem's features) and look for deprecations.

Commit your small in-progress step and repeat as necessary until you can finally pin Rails to 5.0.0.

Once you get those blocking dependencies resolved, and bundle update rails completes successfully, commit that and run your test suite again.

If your suite was anything like mine, there will be a wall of deprecations here, but it's usually just a few things repeated for every test. It isn't strictly necessary to fix these right now, but I would tackle the noisy ones...they may be making it harder to see more important ones. I had to change a ton of controller specs from post :foo, name: 'bar' to post :foo, params: { name: 'bar' } to silence a named parameter syntax deprecation warning.

You may also have to update some code to fix some failing specs here. Luckily, I didn't have to, but you should be able to trace the root cause back to a change in Rails or one of your gems if you updated any.

Now, you should run bin/rails rails:update, to update your configuration files. Carefully diff each of these to look for changes. I prefer to copy/paste lines from the diff output and copy them manually to my configuration and adjust if necessary and keep diffing until all I see are my custom settings in the diff.

After all that is done, run your specs again, and actually open the app and make sure it works as expected. It may be useful to push this to a staging environment to make sure it behaves in a production-like setting, too.

Review the file created by bin/rails rails:update (config/initializers/new_framework_defaults.rb) and see if you can disable or comment these out without affecting your app's behavior.

The behavior of these, as well as other changes are usually documented in the Upgrading Ruby on Rails Guide

Hopefully you are done now, but there are some extra niceties you can bring over. I switch to a new directory and generate a brand new Rails app with rails new rails5project and copy over new files that you probably don't have in your app like app/models/application_record.rb and app/mailers/application_mailer.rb (and transition models to inherit from them instead of ActiveRecord::Base and ActionMailer::Base).

Also, app/assets/javascripts/cable.js, app/assets/javascripts/channels, and app/channels, if you want to use ActionCable, and app/jobs if you want to use ActiveJob`

Then look at the fresh Rails 5 app's Gemfile, and bring over any gems listed there if you want them. For instance, Rails5 now ships with Turbolinks 5, Puma by default, listen and spring-watcher-listen.

Hopefully, after all this you have a working application that still meets all your needs running on Rails 5. However, if you are blocked because of a gem dependency, hopefully you can keep this branch around until everything is ready and merge it!


Just follow this, and make sure you have the minimum Ruby requirements

http://guides.rubyonrails.org/upgrading_ruby_on_rails.html

basic way to do it (not entirely safe but will upgrade it) :

specify in your gemfile

gem 'rails', '5.0.0'

and then in your terminal

bundle update rails