diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 258b2be8992cc6b35f9cc26206cc496df9af5970..e1f543c3a15e88333969ddaa41e3cc2a42e8bdd6 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,10 @@ +* Queries such as `Computer.joins(:monitor).group(:status).count` will now be + interpreted as `Computer.joins(:monitor).group('computers.status').count` + so that when `Computer` and `Monitor` have both `status` columns we don't + have conflicts in projection. + + *Rafael Sales* + * Add ability to default to `uuid` as primary key when generating database migrations Set `Rails.application.config.active_record.primary_key = :uuid` diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 8b9367acc73b50c12651b0530084a31f4a30e61c..6d3e5327fb21256e2264cdfad31bbef9b00aaed3 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -275,6 +275,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc: else group_fields = group_attrs end + group_fields = arel_columns(group_fields) group_aliases = group_fields.map { |field| column_alias_for(field) @@ -299,7 +300,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc: ] select_values += select_values unless having_clause.empty? - select_values.concat arel_columns(group_fields).zip(group_aliases).map { |field,aliaz| + select_values.concat group_fields.zip(group_aliases).map { |field,aliaz| if field.respond_to?(:as) field.as(aliaz) else diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index a0e4b4971b0d71817f8c2cc94f9099cc9e5181b9..4a0e6f497fb7ab84b863449ea197417d9dc847bd 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -455,6 +455,11 @@ def test_should_count_field_in_joined_table_with_group_by [1,6,2,9].each { |firm_id| assert c.keys.include?(firm_id) } end + def test_should_count_field_of_root_table_with_conflicting_group_by_column + assert_equal({ 1 => 1 }, Firm.joins(:accounts).group(:firm_id).count) + assert_equal({ 1 => 1 }, Firm.joins(:accounts).group('accounts.firm_id').count) + end + def test_count_with_no_parameters_isnt_deprecated assert_not_deprecated { Account.count } end