如何解决DEPRECATION警告:类级别的方法将不再从Rails 6.1中继承作用域?

时间:2019-10-31 20:47:44

标签: ruby-on-rails ruby activerecord deprecation-warning ruby-on-rails-6

最近将我的Rails应用程序更新为6.0。在运行测试时,我从Referral模型的范围中收到以下弃用警告:

DEPRECATION WARNING: Class level methods will no longer inherit scoping from `with_all_final_state_fulfillments` in Rails 6.1. To continue using the scoped relation, pass it into the block directly. To instead access the full set of models, as Rails 6.1 will, use `Referral.unscoped`. (called from block in <class:Referral> at /Users/home/workspace/APPNAME/app/models/referral.rb:60)

我所讨论的Referral模型范围很骇人,但写法是这样的:

  scope :with_all_final_state_fulfillments, lambda {
    final_state_ids = Referral.with_fulfillment_in_final_state.pluck(:id).uniq
    not_final_state_ids = Referral.where(id: final_state_ids).with_fulfillment_not_in_final_state.pluck(:id).uniq

    id_list = final_state_ids - not_final_state_ids
    Referral.where(id: id_list)
  }

我已经在互联网上搜索了有关如何解决此问题的建议,including the Rails GitHub PR进行了更改,但是在任何地方都找不到明确的English explanation

如何为Rail 6.1修复此已弃用的范围?

1 个答案:

答案 0 :(得分:2)

通过将Referral.的内部范围调用更新为self.,使过时警告消失:

  scope :with_all_final_state_fulfillments, lambda {
    final_state_ids = self.with_fulfillment_in_final_state.pluck(:id).uniq
    not_final_state_ids = self.where(id: final_state_ids).with_fulfillment_not_in_final_state.pluck(:id).uniq

    id_list = final_state_ids - not_final_state_ids
    where(id: id_list)
  }