Amrit Dhungana
Amrit Dhungana

Reputation: 4485

Rails4 Scope or default_scope deprecation warning

While upgrading to Rails4 from 3.2, I am getting a deprecation warning as

DEPRECATION WARNING: Returning a hash from a #scope or #default_scope block is deprecated. Please return an actual scope object instead. E.g. scope :red, -> { where(color: 'red') } rather than scope :red, -> { { conditions: { color: 'red' } } }. (The scope was defined at /Users/newimac/RailsApp/bank/app/models/account.rb:101.). (called from pry at (pry):18)

When running this Query

Account.need_processing(current)

And

Account.need_processing(@current).should be_empty

And I have defined scope in Rails 3.2 as:

scope :need_processing, lambda {|cutoff| {:conditions => ["state = 'active' and processed_through < ?", cutoff.ago(1.day)]}}

After upgrading to Rails 4.0.4. I have defined scope as

scope :need_processing, -> (cutoff) { where(state: active, processed_through: cutoff.ago(1.day)) }

Check to processed_through between two query in Rails 3.2 and Rails 4.0.

Upvotes: 1

Views: 364

Answers (1)

Zakwan
Zakwan

Reputation: 1072

scope :need_processing, ->(cutoff) {where("state = 'active' and processed_through < ?", cutoff.ago(1.day))}

Upvotes: 1

Related Questions