Tuesday, 9 April 2013

A Ride To Rails 4

        Most of the time we think about a better version of rails for which our application can run smoothly. In the mean time rails has been upgraded to 4 new versions. For rails developer who has been acquainted with all earlier 3 versions will be easy to understand the later versions of rails . Let us discuss some new concepts in rails 4. :)

  1. The most important changes that Rails 4 needs is Ruby 1.9.3 at a minimum.
  2. Vendor/Plugins directory folder has been removed from Rails 4.


Many things has been deprecated and moved to separate gems. Like active record deprecator s finder. Rails 4 uses “activerecord-deprecated_finders” gem for finding old active record finders .

Deprecated finders

Rails 2/3 Model.find([:all/:first/:last]) has been deprecated.

# find_all_by_name
Rails 3 Model.find_all_by_name('name') is deprecated.
In Rails 4, Model.where(:name, 'name')

Rails 3 Model.find_last_by_name('name') is deprecated.
In Rails 4, Model.where(:name, 'name').last

Rails 3 Model.find_or_create_by_name('name') is deprecated.
In Rails 4, Model.where(:name, 'name').first_or_create

Rails 3 Model.find_or_create_by_name!('name') is deprecated.
In Rails 4, Model.where(:name, 'name').first_or_create!

Rails 3 Model.find_or_initialize_by_name('name') is deprecated
In Rails 4, Model.where(:name, 'name').first_or_initialize

Normal Eagerly evaluated Scopes declarations have been deprecated

Scopes require now a callable object. In Rails 4 all scopes must use a callable object such as a Proc or lambda.
For example, In Rails 3 we can declare scope as

Class Comment <ActiveRecord::Base
scope :recent_comment, where(:published_at, Time.now-2.weeks)
but it is deprecated in Rails 4. And the new decalration as below

Class Comment <ActiveRecord::Base
scope :active, -> where(:status, 'active')

Identity map removed

active_record identitymap configuration has been removed from Rails4. If you are upgrading Rails 4 from Rails 3, then please be sure to remove/comment the line from config.application.rb

// config.active_record.identity_map = true


Storing sessions in databases is an interesting feature in many of the applications. As many cases it does not have much influences like a normal cokiee store. So ActiveRecord::SessionStore has been removed from Rails 4. We can use it by adding gem 'activerecord-session_store' in gem file.


ActiveResource which provides a ORM to REST web services is no longer exist in Rails 4. To get the funcationality in your app include gem 'activeresource'


authenticity_token is no longer included in forms in Rails 4. While submiting non-ajax forms in rails, authenticity_token was required in back end to prevent cross-site forgery attacks.

New Features

Postgress Array Support

We all know ProstgreSQL supports array datatypes. That means we can store array elements in a column in PostgreSql. But normally in many of the databases they are stored as strings.

Active Record with Rails4 will support array datatypes for PostgreSql. Also it will support MACADDR, INET, CIDR datatypes.

Strong Parameters

Rails 4 has introduced mass assignment protection of attributes in controller. This process can be achived by the “strong parameters” in Rails 4. It is the tool to fight against the mass assignment vulnerabilities.

Let us consider one example incase of Rails 3, when we are supposed to submit a form to create a user we do as,

    <form method='post' action='/users'>
       <input type='text' name='name' />
       <input type='submit' />

When we submit the form it will go to create a user record. For this we need to mass assign the name attribute in model as 'attr_accessible', neither it will raise an error. To protect the name attribute in model we will do something like this ..

    Class User<ActiveRecord::Base
       attr_accessible :name

Then in controller we will do this to create the user

    def create
      @user = User.new(params[:user])

In Rails 4 there are new options to make the attribute secure and dynamically set them as attr_accessible in controller side.

   Class UsersController< ApplicationController::Base
      def create
         @user = User.new(user_params)

    def user_params

We can test it in our 3.2.x applications by installing the strong_parameters gem in app. :)


Turbolinks makes the following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body and the title in the head.

Turbolink works like PJAX. But there are many fuctionalities difference in both the two.
Turbolinks uses pushState, but instead of replacing only parts a page it loads a complete website from the server and replaces the <title> and<body> in the currently loaded DOM. By default it applies this to all links on a page. So unlike PJAX you don't have to mark links and containers to support in place reloading, Turbolinks will handle that for you.

In Rails 4 bydefault gem 'turbolinks' is being added in the gem file. Hope it will be better to slow down the page responses and the sight will run faster :)

Live Streaming

Rails 4 will support to live streaming. Live stream is the effect to stream content to end user's browser directly from the server with long polling in the browser.

Live streaming has now got much popularity now a days. It helps to watch score reports of various games, sports, watching seminars from famous people hosting through internet, viewing results, and lot more.

The PATCH verb

PUT in REST is defined for updating a record in rails. PATCH will work as same . It will refer to the update action of the controller. But we need to post the form with the parameter “_method=PATCH”. Also routing will help to redirect to the update action in the controller.

 PUT, semantically-speaking, should contain a full representation of an entire object to be updated. But in Rails 4 PATCH will send a new bit of rescource about an object on the server not a full representation of it.

The same PUT method will also work in case of Rails 4.

Concern in Routing

In Rails 3 if we have a same nested resource for a resource, we include the nested resource each time in a parent resouce. For example

    resouces :projects do
        resources :comments

    resources :tasks do
        resources :comments

Rails 4 introduced concern to cut down the duplicacy of common resource routing declaration. Here it defined as

    concern :commentable do
        resources :comments

    resources :projects, concerns: :commentable
    resources :tasks, concerns: :commentable

Debadatt Pradhan
Mindfire Solutions, Pvt. Ltd