aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-12-05 03:26:56 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-12-05 03:26:56 +0000
commit22f095be233e6cfc9bd21104f271d354451bcdcb (patch)
treec00abc0e0511db624a67fdde8bf7fa22d4bef70d /activerecord
parent56267474dac278898bf2f53b9a43fb68bd83f43d (diff)
downloadrails-22f095be233e6cfc9bd21104f271d354451bcdcb.tar.gz
rails-22f095be233e6cfc9bd21104f271d354451bcdcb.tar.bz2
rails-22f095be233e6cfc9bd21104f271d354451bcdcb.zip
Added counter optimization for AssociationCollection#any? so person.friends.any? won't actually load the full association if we have the count in a cheaper form [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5676 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb4
-rwxr-xr-xactiverecord/test/associations_test.rb2
3 files changed, 8 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 2d7b9820ed..fb78e6e539 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added counter optimization for AssociationCollection#any? so person.friends.any? won't actually load the full association if we have the count in a cheaper form [DHH]
+
* Subclasses of an abstract class work with single-table inheritance. #5704 [nick+rails@ag.arizona.edu, Ryan Davis, Jeremy Kemper]
* Change fixture_path to a class inheritable accessor allowing test cases to have their own custom set of fixtures. #6672 [zdennis]
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 73c15ca0a9..273cc64334 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -119,6 +119,10 @@ module ActiveRecord
def empty?
size.zero?
end
+
+ def any?
+ !empty?
+ end
def uniq(collection = self)
seen = Set.new
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 5b5066290f..511653dfce 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -508,7 +508,9 @@ class HasManyAssociationsTest < Test::Unit::TestCase
def test_counting_using_sql
assert_equal 1, Firm.find(:first).clients_using_counter_sql.size
+ assert Firm.find(:first).clients_using_counter_sql.any?
assert_equal 0, Firm.find(:first).clients_using_zero_counter_sql.size
+ assert !Firm.find(:first).clients_using_zero_counter_sql.any?
end
def test_counting_non_existant_items_using_sql