【发布时间】:2014-04-22 15:39:45
【问题描述】:
从 3.2 升级到 Rails4 时,我收到了弃用警告
弃用警告:从 #scope 或 #default_scope 返回哈希 块已弃用。请改为返回一个实际的范围对象。 例如。范围 :red, -> { where(color: 'red') } 而不是范围 :red, -> { { 条件:{ 颜色:'红色' } } }。 (范围定义为 /Users/newimac/RailsApp/bank/app/models/account.rb:101.)。 (从 撬在(撬):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。
【问题讨论】:
-
这是正确的方式 scope :need_processing, -> (cutoff) { where(state: active, "processed_through " => cutoff.ago(1.day)) }
标签: ruby-on-rails-4