Rails Upgrade from 4.2 to 5.0

Reading Time: 2 minutes

It is a good practice to keep an application upto date with the major released versions. Rails 5 is got some exciting features to look out and improve the web development workflow. Here are the key features and steps to upgrade Rails 4.2 to 5.0.1.

Key Features in Rails 5.0.1

1. API only mode
To generate api only app in rails, rails-api gem is merged into Rails.

rails new my-api-only-app — api

It creates an app that generates JSON responsed instead of HTML.

2. ActionCable
ActionCable has been introduced in Rails 5 to provide realtime features in the app. It seamlessly integrates WebSockets with the rest of your Rails application.
Here are the intersting reads about action cable

3. Inheriting ApplicationRecord

All models will inherit from ApplicationRecord, not from ActiveRecord::Base

class Post < ApplicationRecordend

Following file will be automatically added to models in Rails 5 applications. all our customizations can be included here, instead of mokeypatching ActiveRecord::Base.

# app/models/application_record.rbclass ApplicationRecord < ActiveRecord::Base self.abstract_class = trueend

4. Active Record Attributes API

  • Defines an attribute with a type on a model. which helps overriding the type of existing attributes if needed.
  • It controls how values are converted to and from SQL when assigned to a model.
class Item < ApplicationRecord  attribute :price_in_cents, MoneyType.newend
class MoneyType < ActiveRecord::Type::Integer
def type_cast(value)
#logic to implement ‘$2.00’ to 100
item = Item.new(price_in_cents: ‘$2.00’)
item.price_in_cents #=> 100

5. ActiveRecord::Relation#or

#or method has been introduced in ActiveRecord::Relation

User.where(role: ”admin”).or(User.where(role: “merchant”)

6. ActiveRecord::Relation#in_batches

#in_batches yields a relation, whereas #find_in_batches yields an array.

User.in_batches(of: 1000) do |users|
users.update_all(authorized: true)

7. has_secure_token method inclusion

Rails 5 has added has_secure_token method to generate a random alphanumeric token for a given column.

class User < ApplicationRecord
has_secure_token :token1
user = User.createuser.token1 #=> ‘e2426a93718d1817a43abbaa’

8. Turbolinks 5
Turbolinks 3 will not ship with Rails 5 and requires Turbolinks 5. Please read this for further details

9. Require belongs_to by default

belongs_to will now trigger a validation error by default if the association is not present.
You can turn this off on a per-association basis with optional: true
or globally withconfig.active_record.belongs_to_required_by_default = true in initializer

10. Rake to Rails

Rake commands are wrapped using rails to maintain consistency since some of the commands were using rake and some using rails command in Rails earlier versions.

Now rails routes works instead of rake routes

How to upgrade from 4.2 to 5.0

  • Rails 5 now requires Ruby 2.2.2 or greater. So please do install latest ruby 
    rvm install 2.3.1
  • Update gem dependencies to Rails 5

update rails version in gemfile

gem ‘rails’, ‘5.0.1’

  • To update all rails dependencies, please run

bundle update rails

Note: Please verify the support of gems for rails 5 and upgrade accordingly.

  • Update Rails binaries and configurations
rails app:update
  • It updates the app with new Rails 5 configuration changes, please make sure the correct configuration is set according to Rails 4.2 app configuration setup.

Summary :

  • Install Ruby 2.3.1 or higher for rails 5 support.
  • Update the gem dependencies.
  • Update the app binaries and configurations for rails 5.
  • Implement the Rails 5.0.1 new features in app.

Leave a Reply

Your email address will not be published. Required fields are marked *