Skip to content
Browse files

TMP

  • Loading branch information...
1 parent 181b15e commit ac8b621935860f83b778f04f1d5e1b6fc1a4e592 @senny committed Feb 7, 2013
View
29 activerecord/lib/active_record/relation/calculations.rb
@@ -96,19 +96,19 @@ def sum(*args)
#
# Person.sum("2 * age")
def calculate(operation, column_name, options = {})
+ puts "\n{"
relation = with_default_scope
+ return relation.calculate(operation, column_name, options) unless relation.equal?(self)
- if relation.equal?(self)
- if has_include?(column_name)
- construct_relation_for_association_calculations.calculate(operation, column_name, options)
- else
- perform_calculation(operation, column_name, options)
- end
- else
- relation.calculate(operation, column_name, options)
+ if has_include?(column_name)
+ return construct_relation_for_association_calculations.calculate(operation, column_name, options)
end
+
+ perform_calculation(operation, column_name, options)
rescue ThrowResult
0
+ ensure
+ puts "}"
end
# Use <tt>pluck</tt> as a shortcut to select one or more attributes without
@@ -199,7 +199,7 @@ def perform_calculation(operation, column_name, options = {})
distinct = options[:distinct] || self.uniq_value
if operation == "count"
- column_name ||= (select_for_count || :all)
+ column_name ||= select_for_count
unless arel.ast.grep(Arel::Nodes::OuterJoin).empty?
distinct = true
@@ -366,10 +366,13 @@ def type_cast_using_column(value, column)
end
def select_for_count
- if select_values.present?
- select = select_values.join(", ")
- select if select !~ /[,*]/
- end
+ return :all if select_values.blank?
+ return :all if select_values.size > 1
+
+ column = select_values.first
+ return :all if column =~ /[,*]/
+
+ column
end
def build_count_subquery(relation, column_name, distinct)
View
4 activerecord/test/cases/calculations_test.rb
@@ -337,6 +337,10 @@ def test_should_count_manual_select_with_include
assert_equal 6, Account.select("DISTINCT accounts.id").includes(:firm).count
end
+ def test_should_invalid_count_falls_back_on_count_all
+ assert_equal 6, Account.select("accounts.id, accounts.firm_name").count
+ end
+
def test_count_with_column_parameter
assert_equal 5, Account.count(:firm_id)
end

0 comments on commit ac8b621

Please sign in to comment.
Something went wrong with that request. Please try again.