Wednesday, August 21, 2013

Rails 4 - Finders

Old-Style finders are deprecated
In Rails 4, Old-Style finders are deprecated. Consider User model with name, age members
Class User < ActiveRecord::Base
  attr_accessor :name,:age

  def initialize(name, age)
    @name = name
    @age = age
  end
end

In this class, If we want find user with name of 'foo'. See the difference in rails 3 and rails 4 call
#Rails 3
User.find(:all, conditions: { name: 'foo' })
#Rails 4
User.where(name: 'foo')

In rails 4, you will get DEPRECATION WARNING: Calling #find(:all) is deprecated. You can call #all directly instead or build a scope instead of using finder options.

Dynamic finders that return collections are deprecated
#Rails 3
User.find_all_by_name('foo')
#Rails 4
User.where(name: 'foo')

In Rails 4 alerts DEPRECATION WARNING if we use dynamic method(User.find_all_by_name). Preferable alternative option is User.where(...).all

No comments:

Post a Comment