diff options
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 7 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 16 |
3 files changed, 17 insertions, 8 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 0ee5f91532..ddbb25e9de 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,6 +1,6 @@ *SVN* -* Fixed that eager loading queries should respect the :group option as well [DHH] +* Fixed that eager loading queries and with_scope should respect the :group option [DHH] * Improve performance and functionality of the postgresql adapter. Closes #8049 [roderickvd] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index a5da8db206..438e96a8ce 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1292,8 +1292,7 @@ module ActiveRecord add_conditions!(sql, options[:conditions], scope) add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit]) - sql << "GROUP BY #{options[:group]} " if options[:group] - + add_group!(sql, options[:group], scope) add_order!(sql, options[:order], scope) add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections) add_lock!(sql, options, scope) @@ -1333,13 +1332,13 @@ module ActiveRecord end add_conditions!(sql, options[:conditions], scope) - sql << " GROUP BY #{options[:group]} " if options[:group] + add_group!(sql, options[:group], scope) if options[:order] if is_distinct connection.add_order_by_for_association_limiting!(sql, options) else - sql << "ORDER BY #{options[:order]}" + add_order!(sql, options[:order], scope) end end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 61ba555553..44cbbc97cc 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1112,8 +1112,7 @@ module ActiveRecord #:nodoc: add_joins!(sql, options, scope) add_conditions!(sql, options[:conditions], scope) - sql << " GROUP BY #{options[:group]} " if options[:group] - + add_group!(sql, options[:group], scope) add_order!(sql, options[:order], scope) add_limit!(sql, options, scope) add_lock!(sql, options, scope) @@ -1148,6 +1147,17 @@ module ActiveRecord #:nodoc: sql << " ORDER BY #{scoped_order}" if scoped_order end end + + def add_group!(sql, group, scope = :auto) + scope = scope(:find) if :auto == scope + scoped_group = scope[:group] if scope + + if group + sql << " GROUP BY #{group}" + elsif scoped_group + sql << " GROUP BY #{scoped_group}" + end + end # The optional scope argument is for the current :find scope. def add_limit!(sql, options, scope = :auto) @@ -1383,7 +1393,7 @@ module ActiveRecord #:nodoc: method_scoping.assert_valid_keys([ :find, :create ]) if f = method_scoping[:find] - f.assert_valid_keys([ :conditions, :joins, :select, :include, :from, :offset, :limit, :order, :readonly, :lock ]) + f.assert_valid_keys([ :conditions, :joins, :select, :include, :from, :offset, :limit, :order, :group, :readonly, :lock ]) set_readonly_option! f end |