You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.adoc
+19Lines changed: 19 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1069,6 +1069,25 @@ User.all.size
1069
1069
User.all.length
1070
1070
----
1071
1071
1072
+
=== Check if the collection is empty [[check-empty-collection]]
1073
+
1074
+
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.
1075
+
1076
+
[source,ruby]
1077
+
----
1078
+
# bad
1079
+
users.where(active: true).present?
1080
+
users.where(active: true).blank?
1081
+
1082
+
# good
1083
+
users.where(active: true).any?
1084
+
users.where(active: true).empty?
1085
+
1086
+
# good - the collection is used after loading
1087
+
users.present?
1088
+
users.each(&:notify)
1089
+
----
1090
+
1072
1091
=== Where with Ranges [[where-ranges]]
1073
1092
1074
1093
Use ranges instead of defining comparative conditions using a template for scalar values.
0 commit comments