From 4e2bec383239f9ab3493208ef3bad1f2cbea3c93 Mon Sep 17 00:00:00 2001 From: David Celis Date: Thu, 9 May 2013 20:31:31 +0530 Subject: 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 --- activerecord/lib/active_record/relation.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'activerecord/lib') 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 -- cgit v1.2.3