Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix DEPRECATION WARNING: Class level methods will no longer inherit scoping from in Rails 6.1?

Recently updated my Rails app to 6.0. When I run my tests I get the following deprecation warning from a scope on my Referral model:

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)

My Referral model scope in question is hacky, but written like this:

  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)
  }

I've searched all around the internet for advice on how to fix this deprecation, including the Rails GitHub PR's making the change, but haven't found a clear English explanation anywhere.

How do I fix this deprecated scope for Rail 6.1?

like image 354
Kelsey Hannan Avatar asked Oct 31 '19 20:10

Kelsey Hannan


1 Answers

Made the deprecation warning go away by updating inside scope calls of 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)
  }
like image 148
Kelsey Hannan Avatar answered Oct 22 '22 03:10

Kelsey Hannan