At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

Upgrading to Rails 5.0 from Rails 4.2

  • By Saravana
  • December 25, 2016
  • 859 Views

Why to Upgrade?

Rails 5.0 has a lot of new features. It addresses many issues and dealing with performance, security and new features like ActionCable, Rails API, Turbolinks 3 and etc. Check this out for more details: http://weblog.rubyonrails.org/2016/6/30/Rails-5-0-final/
Prerequisites
Rails 5.0 requires Ruby 2.2.2 or greater versions
Upgrade your application to Rails 4.2
Decent code coverage to make upgrade easier
Let’s do the steps for an upgrade, now:

1) Update ruby

Rails 5.0 requires ruby greater than 2.2.2. So install latest stable ruby version using RVM


	$ rvm install 2.3.1

2) Update your gem dependencies to rails 5

Change rails version in Gemfile


		gem 'rails', '~> 5.0', '>= 5.0.0.1'

Update Rails dependencies


		$ bundle update rails

3) Update rails binaries and configurations

Run the following command for updating rails 5.0 configurations. This will add/modify the changes in bin/ and /config folders.


		$ rails app:update

4) Generate DB schema rb file

Rails moved foreign and unique indexes to inside the create_table methods. To include these changes run migration again. No need to have a pending migration to generate a new schema.

5) Application classes

Rails 5.0 has new application classes that you should have and inherit from, the same way as ApplicationController or ApplicationHelper. Those are:
ApplicationRecord
ApplicationMailer
ApplicationJob

ApplicationRecord

All your models should inherit from ApplicationRecord


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

ApplicationMailer

All your mailers should inherit from ApplicationMailer. Previous rails versions don’t have layouts for mailers. Create mailer layout under layouts directory.


			# app/mailers/application_mailer.rb
			class ApplicationMailer < ActionMailer::Base
				layout 'mailer'
			end

ApplicationJob

Change your jobs to inherit from your new ApplicationJob


  # app/jobs/application_job.rb
  class ApplicationJob < ActiveJob::Base
  end

6) Validation changes

Rails 5.0 has changed the belongs_to relation to require presence by default.


belongs_to :model, required: true

You have to create an initializer to explicitly configure the new behavior for the app


#config/initializers/active_record_belongs_to_required_by_default.rb
Rails.application.config.active_record.belongs_to_required_by_default = true

And for every belongs_to relation that is not required, just add optional: true.


	belongs_to :model, optional: true

7) Halt callback chain – throw(:abort)

Active Recor d or Active Model callbacks no longer halt when returning false. You should create initializer to explicitly configure this


# config/initializers/callback_terminator.rb
ActiveSupport.halt_callback_chains_on_return_false = false

Then you have to handle false manually like below example


class Person < ApplicationRecord
		before_create do
			throw(:abort) if you_need_to_halt
		end
end

Conclusion

After all the above changes are implemented, we can run our application in Rails 5.0. As mentioned, If you have a decent code coverage then the upgrade will not be a big problem. However, check whether all gems supporting, are up-to-date. To know more about Ruby on Rails web application development follow Agira Technologies.

Saravana

An enthusiastic Tech Lead with 7 plus years of experience in Web development arena. Owns legitimate experience in Ruby, Ruby On Rails, AngularJs, DevOps. Golang, Another add on, This young tech freak never miss a chance to get his hands on planting and Gardening even in his busy weekends.