diff options
author | Xavier Noria <fxn@hashref.com> | 2008-08-21 15:45:06 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-08-21 15:48:04 +0100 |
commit | a970f916fb1e05376733e2d42d9bcc2b873af355 (patch) | |
tree | 32341e068add430be9687f38dc08722820d45845 /activerecord/lib | |
parent | ea40f71431a821b2ddb37be6ea3ee7d8dac63b85 (diff) | |
download | rails-a970f916fb1e05376733e2d42d9bcc2b873af355.tar.gz rails-a970f916fb1e05376733e2d42d9bcc2b873af355.tar.bz2 rails-a970f916fb1e05376733e2d42d9bcc2b873af355.zip |
Fix has_many#count_records. [#865 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index e6fa15c173..ce62127505 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -35,8 +35,11 @@ module ActiveRecord else @reflection.klass.count(:conditions => @counter_sql, :include => @reflection.options[:include]) end - - @target = [] and loaded if count == 0 + + # If there's nothing in the database and @target has no new records + # we are certain the current target is an empty array. This is a + # documented side-effect of the method that may avoid an extra SELECT. + @target ||= [] and loaded if count == 0 if @reflection.options[:limit] count = [ @reflection.options[:limit], count ].min |