最近将我的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修复此已弃用的范围?
答案 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)
}