From 9a1323aa4c4ccfac10ccb9b77b8817e232f8338b Mon Sep 17 00:00:00 2001 From: fatkodima Date: Wed, 10 Sep 2025 18:30:53 +0300 Subject: [PATCH] Add suggestions for checking if collection is empty --- README.adoc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.adoc b/README.adoc index cf59a43..7ce2351 100644 --- a/README.adoc +++ b/README.adoc @@ -1069,6 +1069,25 @@ User.all.size User.all.length ---- +=== Check if the collection is empty [[check-empty-collection]] + +When checking if the unloaded Active Record collection is empty, prefer `any?`/`empty?` over `present?`/`blank?`. The former executes a simple `EXISTS`-like query while the latter loads the whole collection to determine its size. + +[source,ruby] +---- +# bad +users.where(active: true).present? +users.where(active: true).blank? + +# good +users.where(active: true).any? +users.where(active: true).empty? + +# good - the collection is used after loading +users.present? +users.each(&:notify) +---- + === Where with Ranges [[where-ranges]] Use ranges instead of defining comparative conditions using a template for scalar values.