From dc8bf7515de85f5bc28d17e96edf4a3e74a858da Mon Sep 17 00:00:00 2001 From: miloops Date: Mon, 15 Sep 2008 13:23:50 -0300 Subject: When counting grouped records the target should be loaded to return a valid groups count result. Without this change count_records will group for the count in the query and return erroneous results. Signed-off-by: Michael Koziarski [#937 state:committed] --- activerecord/lib/active_record/associations/association_collection.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 8de528f638..afb817f8ae 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -238,6 +238,8 @@ module ActiveRecord def size if @owner.new_record? || (loaded? && !@reflection.options[:uniq]) @target.size + elsif !loaded? && @reflection.options[:group] + load_target.size elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array) unsaved_records = @target.select { |r| r.new_record? } unsaved_records.size + count_records -- cgit v1.2.3