diff options
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/calculations.rb | 39 |
2 files changed, 29 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 52cad79484..56368f4198 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -36,15 +36,18 @@ module ActiveRecord end # Count the number of associated records. All arguments are optional. - def count(runtime_conditions = nil) + def count(*args) if @reflection.options[:counter_sql] @reflection.klass.count_by_sql(@counter_sql) elsif @reflection.options[:finder_sql] @reflection.klass.count_by_sql(@finder_sql) else - sql = @finder_sql - sql += " AND (#{sanitize_sql(runtime_conditions)})" if runtime_conditions - @reflection.klass.count(sql) + column_name, options = @reflection.klass.send(:construct_count_options_from_legacy_args, *args) + options[:conditions] = options[:conditions].nil? ? + @finder_sql : + @finder_sql + " AND (#{sanitize_sql(options[:conditions])})" + + @reflection.klass.count(column_name, options) end end diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 863aedbf9c..acab77870f 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -42,23 +42,7 @@ module ActiveRecord # # Note: Person.count(:all) will not work because it will use :all as the condition. Use Person.count instead. def count(*args) - options = {} - column_name = :all - # For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}) or count(column_name=:all, options={}). - if args.size >= 0 && args.size <= 2 - if args.first.is_a?(Hash) - options = args.first - elsif args[1].is_a?(Hash) - options = args[1] - column_name = args.first - else - # Handle legacy paramter options: def count(conditions=nil, joins=nil) - options.merge!(:conditions => args[0]) if args.length > 0 - options.merge!(:joins => args[1]) if args.length > 1 - end - else - raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})") - end + column_name, options = construct_count_options_from_legacy_args(*args) if options[:include] || scope(:find, :include) count_with_associations(options) @@ -146,6 +130,27 @@ module ActiveRecord end protected + def construct_count_options_from_legacy_args(*args) + options = {} + column_name = :all + # For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}) or count(column_name=:all, options={}). + if args.size >= 0 && args.size <= 2 + if args.first.is_a?(Hash) + options = args.first + elsif args[1].is_a?(Hash) + options = args[1] + column_name = args.first + else + # Handle legacy paramter options: def count(conditions=nil, joins=nil) + options.merge!(:conditions => args[0]) if args.length > 0 + options.merge!(:joins => args[1]) if args.length > 1 + end + else + raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})") + end + [column_name, options] + end + def construct_calculation_sql(aggregate, aggregate_alias, options) #:nodoc: scope = scope(:find) sql = "SELECT #{aggregate} AS #{aggregate_alias}" |