Skip to content

Conversation

fatkodima
Copy link
Contributor

README.adoc Outdated
@@ -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 it's size.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 it's size.
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.

@fatkodima fatkodima force-pushed the check-empty-collection branch from ea8d186 to 9a1323a Compare September 10, 2025 15:50
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is even more descriptive.
user.articles.any? is used just as frequently as users.where(...).any?, and in the former case, any? behavior is not equivalent to exists?, and is not behaving similarly to empty?.

users.where(active: true).empty?

# good - the collection is used after loading
users.present?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced that preloading the list of records implicitly with present? is a practice we should recommend to everyone.

@@ -1069,6 +1069,25 @@ User.all.size
User.all.length
----

=== Check if the collection is empty [[check-empty-collection]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is generic. The guideline is specific to "not loaded collection".
It is hard to know if it's loaded or not if it e.g. comes as an argument to a method, or elsewhere:

def highlight?
  recent_articles.any?
end

users.where(active: true).empty?

# good - the collection is used after loading
users.present?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are hundreds of thousands of those records?

users.where(active: true).any?
users.where(active: true).empty?

# good - the collection is used after loading
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am referring to the "used" wording above.

Even though the collection will be used a few lines below, maybe only a fraction of it will be actually used with a batching iterator like `users.find_each { ...?

Or it will be further queried upon e.g. users.active? Or users.last(3).

Or, it will be used in a sub-query, and it doesn't meet the purpose to load them all e.g. Articles.recent.joins(:author).merge(users).


# good - the collection is used after loading
users.present?
users.each(&:notify)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fatkodima
Copy link
Contributor Author

@pirj If you have suggestions for better wording/examples, I think it is better to force push into the PR. I used simple examples (not caring about each vs find_each etc) just for example purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants