Rails4 Scope或default_scope弃用警告

时间:2014-03-17 12:28:21

标签: ruby-on-rails-4

从3.2升级到Rails4时,我收到了弃用警告

  

DEPRECATION WARNING:从#scope或#default_scope返回哈希值   块已弃用。请改为返回实际范围对象。   例如。范围:红色, - > {where(color:'red')}而不是范围:红色, - >   {{conditions:{color:'red'}}}。 (范围定义于   /Users/newimac/RailsApp/bank/app/models/account.rb:101)。 (来自    pry at(pry):18)

运行此查询时

Account.need_processing(current)

并且

Account.need_processing(@current).should be_empty

我在Rails 3.2中将scope定义为:

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

升级到Rails 4.0.4之后。我已将scope定义为

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

在Rails 3.2和Rails 4.0中的两个查询之间检查processed_through

1 个答案:

答案 0 :(得分:1)

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