diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 3c05c52..c52e58e 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1472,7 +1472,8 @@ module ActiveRecord # Only join tables referenced in order or conditions since this is particularly slow on the pre-query. tables_from_conditions = conditions_tables(options) tables_from_order = order_tables(options) - all_tables = tables_from_conditions + tables_from_order + tables_from_group = group_tables(options) + all_tables = tables_from_conditions + tables_from_order + tables_from_group distinct_join_associations = all_tables.uniq.map{|table| join_dependency.joins_for_table_name(table) }.flatten.compact.uniq @@ -1482,7 +1483,7 @@ module ActiveRecord order = order ? "#{order}, #{scoped_order}" : scoped_order end - is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order) + is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order) || include_eager_group?(options, tables_from_group) sql = "SELECT " if is_distinct sql << connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", order) @@ -1534,6 +1535,12 @@ module ActiveRecord select.scan(/"?([\.a-zA-Z_]+)"?.?\./).flatten end + def group_tables(options) + group = [options[:group], scope(:find, :group) ].join(", ") + return [] unless group && group.is_a?(String) + group.scan(/([\.a-zA-Z_]+).?\./).flatten + end + # Checks if the conditions reference a table other than the current model table def include_eager_conditions?(options, tables = nil) ((tables || conditions_tables(options)) - [table_name]).any? @@ -1544,12 +1551,17 @@ module ActiveRecord ((tables || order_tables(options)) - [table_name]).any? end + # Checks if the query group references a table other than the current model's table. + def include_eager_group?(options, tables = nil) + ((tables || group_tables(options)) - [table_name]).any? + end + def include_eager_select?(options) (selects_tables(options) - [table_name]).any? end def references_eager_loaded_tables?(options) - include_eager_order?(options) || include_eager_conditions?(options) || include_eager_select?(options) + include_eager_order?(options) || include_eager_conditions?(options) || include_eager_select?(options) || include_eager_group?(options) end def using_limitable_reflections?(reflections) diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 6fec290..0809a49 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -640,4 +640,8 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_order_on_join_table_with_include_and_limit assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size end + + def test_group_on_join_table_with_include_and_limit + assert_equal 2, Developer.find(:all, :include => 'projects', :group => 'projects.name', :limit => 5).size + end end