Open tagliala opened 1 year ago
Note, It can be changed from !Model.exists?
to Model.none?
when a direct method call is made to model class, but cannot be changed to Model.empty?
.
Model.empty? #=> undefined method `empty?'
Also:
user = User.new
user.posts.new
!user.posts.exists?
=> true
user.posts.none?
=> false
Heads-up
none?
is not the same as !exists?
. It is optimized and will not perform a query everytime
⚠️ Disclaimer: please double check if this proposal makes sense
Is your feature request related to a problem? Please describe.
A new cop to check for the usage of
none?
instead of!exists?
when no conditions are given.Describe the solution you'd like
The cop should suggest the following change:
This should not be triggered if
exists?
has parameters, becauseempty?
andnone?
do not support argumentsAdditional context
I've tried to read the source code of AR, and it appears that
none?
and!exists?
are not the same, becauseempty?
will avoid an extraSELECT 1 AS one
query if the records are already loadedI've tested if there are performance differences with:
In case the association is not preloaded (same scenario without
includes(:addresses)
), for this use case, the result is the same