Skip to content

Commit

Permalink
"empty?" docs: Suggest "load.empty?" over "length.zero?" [ci skip]
Browse files Browse the repository at this point in the history
If "collection.empty?" is what you wanted to do, but you need to work around a collection-loading gotcha, "collection.load.empty?" is much closer to the intended code than changing the method entirely to do "collection.length.zero?" instead.

Read more: https://thepugautomatic.com/2021/02/using-load-with-any-to-avoid-double-queries-from-activerecord/
  • Loading branch information
henrik authored Feb 28, 2021
1 parent d1b36e0 commit 9a00d89
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ def size
# to <tt>collection.size.zero?</tt>. If the collection has not been loaded,
# it is equivalent to <tt>!collection.exists?</tt>. If the collection has
# not already been loaded and you are going to fetch the records anyway it
# is better to check <tt>collection.length.zero?</tt>.
# is better to check <tt>collection.load.empty?</tt>.
#
# class Person < ActiveRecord::Base
# has_many :pets
Expand Down

0 comments on commit 9a00d89

Please sign in to comment.