From 4da1a98ea10d505adf8201d9ecfc6065507652d7 Mon Sep 17 00:00:00 2001 From: Joshua Krall Date: Mon, 22 Jun 2009 02:30:52 -0500 Subject: [PATCH] Fixed problem with Model.count(:select=>'a,b') --- activerecord/lib/active_record/calculations.rb | 3 ++- activerecord/test/cases/calculations_test.rb | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 727f4c1..711ce0f 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -125,6 +125,7 @@ module ActiveRecord validate_calculation_options(operation, options) column_name = options[:select] if options[:select] column_name = '*' if column_name == :all + column_name = column_name.split(/,/)[0] if column_name.is_a?(String) # with :select=>'a,b' options, only take the first col column = column_for column_name catch :invalid_query do if options[:group] @@ -187,7 +188,7 @@ module ActiveRecord end if options[:distinct] && column_name.to_s !~ /\s*DISTINCT\s+/i - distinct = 'DISTINCT ' + distinct = 'DISTINCT ' end sql = "SELECT #{operation}(#{distinct}#{column_name}) AS #{aggregate_alias}" diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 75f52df..a246fbd 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -25,7 +25,7 @@ class CalculationsTest < ActiveRecord::TestCase def test_should_return_nil_as_average assert_nil NumericData.average(:bank_balance) end - + def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal assert_equal 0, NumericData.send(:type_cast_calculated_value, 0, nil, 'avg') assert_equal 53.0, NumericData.send(:type_cast_calculated_value, 53, nil, 'avg') @@ -269,6 +269,11 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal 0, Account.scoped(:select => "credit_limit").count end + def test_should_count_scoped_select_with_multiple_columns + Account.update_all("credit_limit = NULL") + assert_equal 0, Account.scoped(:select => "credit_limit, COS(credit_limit) as cosine_of_credit_limit").count + end + def test_should_count_scoped_select_with_options Account.update_all("credit_limit = NULL") Account.last.update_attribute('credit_limit', 49) -- 1.6.1