aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDavid Celis <me@davidcel.is>2013-05-09 20:31:31 +0530
committerDavid Celis <me@davidcel.is>2013-06-07 10:16:59 -0700
commit4e2bec383239f9ab3493208ef3bad1f2cbea3c93 (patch)
treeaa7c08c45b9cc6ee85a3f896c0f6cf23300ef8cb /activerecord/lib
parent94725b81f5588e4b0f43222c4f142c3135941b4b (diff)
downloadrails-4e2bec383239f9ab3493208ef3bad1f2cbea3c93.tar.gz
rails-4e2bec383239f9ab3493208ef3bad1f2cbea3c93.tar.bz2
rails-4e2bec383239f9ab3493208ef3bad1f2cbea3c93.zip
ActiveRecord::Relation#blank? should `LIMIT 1`
This is an SQL improvement to ActiveRecord::Relation#blank?. Currently, it calls `to_a` on the Relation, which loads all records in the association, and calls `blank?` on the loaded Array. There are other ways, however, to check the emptiness of an association that are far more performant. `#empty?`, `#exists?` and `#any?` all attach a `LIMIT 1` to the SQL query before firing it off, which is a nice query improvement. `#blank?` should do the same! Bonus performance improvements will also happen for `#present?`, which merely calls the negation of `#blank?` Signed-off-by: David Celis <me@davidcel.is>
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation.rb6
1 files changed, 1 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index d54479edbb..5e525a49c0 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -21,6 +21,7 @@ module ActiveRecord
alias :model :klass
alias :loaded? :loaded
alias :default_scoped? :default_scoped
+ alias :blank? :empty?
def initialize(klass, table, values = {})
@klass = klass
@@ -575,11 +576,6 @@ module ActiveRecord
end
end
- # Returns true if relation is blank.
- def blank?
- to_a.blank?
- end
-
def values
Hash[@values]
end