如何修复弃用警告:类级别方法将不再继承 Rails 6.1 中的作用域?

Kel*_*nan 11 ruby activerecord ruby-on-rails 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)
Run Code Online (Sandbox Code Playgroud)

我的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)
  }
Run Code Online (Sandbox Code Playgroud)

我在互联网上搜索了有关如何解决此弃用问题的建议,包括 Rails GitHub PR 进行了更改,但在任何地方都没有找到明确的英文解释

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

Kel*_*nan 16

通过更新Referral.to 的内部范围调用,使弃用警告消失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)
  }
Run Code Online (Sandbox Code Playgroud)

  • 另一种可能的解决方案可能是使用 [Referral.default_scoped.x](https://github.com/rails/rails/pull/35280#issuecomment-585516268) (4认同)