diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-12-28 19:11:02 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-12-28 19:11:02 -0800 |
commit | 54a65183e706d324fd220b5ff46c9a8dc27bfe89 (patch) | |
tree | 514b17a7849175876314b06763785875aea0df92 | |
parent | ac6594921f4d8532b41a956e90ca56aa05f941be (diff) | |
download | rails-54a65183e706d324fd220b5ff46c9a8dc27bfe89.tar.gz rails-54a65183e706d324fd220b5ff46c9a8dc27bfe89.tar.bz2 rails-54a65183e706d324fd220b5ff46c9a8dc27bfe89.zip |
fix PG typecasting errors
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 27 |
2 files changed, 12 insertions, 22 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index ccc14dddeb..e27c64e3c9 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -241,13 +241,16 @@ module ActiveRecord select_value = operation_over_aggregate_column(column, operation, distinct) + column_name = select_value.alias relation.select_values = [select_value] query_builder = relation.arel end - result = @klass.connection.select_value(query_builder, nil, relation.bind_values) - type_cast_calculated_value(result, column_for(column_name), operation) + result = @klass.connection.select_all(query_builder, nil, relation.bind_values) + row = result.first + value = row && row.values.first + type_cast_calculated_value(value, result.column_types[column_name], operation) end def execute_grouped_calculation(operation, column_name, distinct) #:nodoc: diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 5cb7eabf0e..b7622705bf 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -8,6 +8,7 @@ require 'models/possession' require 'models/topic' require 'models/minivan' require 'models/speedometer' +require 'models/ship_part' Company.has_many :accounts @@ -33,8 +34,9 @@ class CalculationsTest < ActiveRecord::TestCase end def test_should_return_integer_average_if_db_returns_such - Account.connection.stubs :select_value => 3 - value = Account.average(:id) + ShipPart.delete_all + ShipPart.create!(:id => 3, :name => 'foo') + value = ShipPart.average(:id) assert_equal 3, value end @@ -416,34 +418,19 @@ class CalculationsTest < ActiveRecord::TestCase def test_maximum_with_not_auto_table_name_prefix_if_column_included Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)]) - # TODO: Investigate why PG isn't being typecast - if current_adapter?(:PostgreSQLAdapter) || current_adapter?(:MysqlAdapter) - assert_equal "7", Company.includes(:contracts).maximum(:developer_id) - else - assert_equal 7, Company.includes(:contracts).maximum(:developer_id) - end + assert_equal 7, Company.includes(:contracts).maximum(:developer_id) end def test_minimum_with_not_auto_table_name_prefix_if_column_included Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)]) - # TODO: Investigate why PG isn't being typecast - if current_adapter?(:PostgreSQLAdapter) || current_adapter?(:MysqlAdapter) - assert_equal "7", Company.includes(:contracts).minimum(:developer_id) - else - assert_equal 7, Company.includes(:contracts).minimum(:developer_id) - end + assert_equal 7, Company.includes(:contracts).minimum(:developer_id) end def test_sum_with_not_auto_table_name_prefix_if_column_included Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)]) - # TODO: Investigate why PG isn't being typecast - if current_adapter?(:MysqlAdapter) || current_adapter?(:PostgreSQLAdapter) - assert_equal "7", Company.includes(:contracts).sum(:developer_id) - else - assert_equal 7, Company.includes(:contracts).sum(:developer_id) - end + assert_equal 7, Company.includes(:contracts).sum(:developer_id) end |