Running Rails 4, Model.scoped now deprecated.
DEPRECATION WARNING: Model.scoped is deprecated. Please use Model.all instead.
But there is a difference in Model.scoped and Model.all , that is, scoped.scoped returns the scope, and all.all launches the request.
In Rails 3:
> Model.scoped.scoped.is_a?(ActiveRecord::Relation) => true
In Rails 4:
> Model.all.all.is_a?(ActiveRecord::Relation) DEPRECATION WARNING: Relation
There are usage examples in libraries / issues that return scoped when there is a condition to do something or nothing, for example:
module AmongConcern extend ActiveSupport::Concern module ClassMethods def among(ids) return scoped if ids.blank? where(id: ids) end end end
If you change this scoped to all , you will run into random problems depending on where among used in the scope chain. For example, Model.where(some: value).among(ids) will run a query instead of returning a scope.
I want to use the idempotent method on ActiveRecord::Relation , which simply returns the scope.
What am I supposed to do here?
activerecord ruby-on-rails-4
kenn Aug 13 2018-12-13T00: 00Z
source share