Shiva Kumar 's Blog

Start small,Think Big

Rails 4 new features

In this post will be explaining some new stuffs ,changes and deprecation in rails 4

Lets start with concerns.In Rails 3, as you can see comments and tags are nested routes for coupons and posts which has a quite of lot duplication in code

resources :coupons do
resources :comments
resources :tags

resources :posts do
resources :comments
resources :tags

In Rails 4 we use concerns,to avoid this duplications

concern :sociable do
resources :comments
resources :tags

resources :coupons, concerns: :sociable
resources :posts, concerns: :sociable

Sometimes when want to pass option to our concern block we can do that by passing argument to the block

concerns :sociable do |options|
resources :comments,options
resources :tags,options
resources items do
concerns :sociable,only: :create

In rails 3 finder and dynamic finder (eg Post.find or Post.find_all_by_title are deprecated instead use active-relation api) .If you still want to use old style finder you can use gem called activerecord-deprecated-finders
And Post.find_by_title will work find but in rails 4 there is new find_by method in which we pass hash like this

Post.find_by(:title => "Hello")

Even find_by method condition arguments are deprecated in rails 3

Post.find_by_title("hello",:conditions: {author: 'moderator'})

In rails 4,we do it passing argument as hash

Post.find_by(title: "hello",author: "moderator")

Dynamic find which create or initialize a object are also deprecated.In rails 3 we do something like

Post.find_or_initialize_by_title("New world")
Post.find_or_create_by_title("New world")

Rails 4
Post.find_or_create_by(:title "New world")
Post.find_or_initialize_by(:title "New world")

Update attribute are deprecated to just update so instead of using

Rails 3

Rails 4


In Rails 4 Strong params , attr_accessible is deprecated in rails 3, attr_accessible is used to allow attributes to be set via mass-assignment .It only accepts attributes that setted as attr_accessible in model so that no other attribute is accepted when submitting the form
For eg

class User < ActiveRecord::Base
attr_accessible :name

So in rails 4 the whitelisting of parameter takes place in controller than in model

def create
user_params = params.require(:user).permit(:name)!

params.require we are checking/validating whether the user key existing it will a return an hash since we are permitting with name parameter we get the name hash if no parameter is set just params.require(:user) will return an empty hash.If any invalid params are send by user it gonna strip out

user_params = params.require(:user).permit(:name)

Can cause duplication of code in your controller we can move that to a private method like this and use it

class UsersController < ApplicationController
def create!

def user_params


There more great new features in rails 4 will be explaining in future posts

, ,

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">