aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-04-23 18:18:18 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-04-23 18:18:18 +0000
commit56412f4441f0954d3181eafbfb7b60b361e05d11 (patch)
tree99052526fd70cf0ebf3029f0bf7c166d5348ac85 /activerecord
parent3cf461323c0401e56e4a7d7962745bbbe5316fd1 (diff)
downloadrails-56412f4441f0954d3181eafbfb7b60b361e05d11.tar.gz
rails-56412f4441f0954d3181eafbfb7b60b361e05d11.tar.bz2
rails-56412f4441f0954d3181eafbfb7b60b361e05d11.zip
Optimize counting of has_many associations by setting the association to empty if the count is 0
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1235 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb6
1 files changed, 5 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index 5b0ac25d17..31b8ac6b9e 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -92,13 +92,17 @@ module ActiveRecord
end
def count_records
- if has_cached_counter?
+ count = if has_cached_counter?
@owner.send(:read_attribute, cached_counter_attribute_name)
elsif @options[:counter_sql]
@association_class.count_by_sql(@counter_sql)
else
@association_class.count(@counter_sql)
end
+
+ @target = [] and loaded if count == 0
+
+ return count
end
def has_cached_counter?