Skip to content

Commit

Permalink
Ignore order when doing count.
Browse files Browse the repository at this point in the history
This is necessary because Postgresql doesn't play nice with ORDER BY and
no GROUP BY.

Fixes #14621.
  • Loading branch information
laurocaetano committed Apr 7, 2014
1 parent 013b7c1 commit bbad752
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def operation_over_aggregate_column(column, operation, distinct)


def execute_simple_calculation(operation, column_name, distinct) #:nodoc: def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
# Postgresql doesn't like ORDER BY when there are no GROUP BY # Postgresql doesn't like ORDER BY when there are no GROUP BY
relation = reorder(nil) relation = unscope(:order)


column_alias = column_name column_alias = column_name


Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -387,6 +387,20 @@ def test_count_with_too_many_parameters_raises
assert_raise(ArgumentError) { Account.count(1, 2, 3) } assert_raise(ArgumentError) { Account.count(1, 2, 3) }
end end


def test_count_with_order
assert_equal 6, Account.order(:credit_limit).count
end

def test_count_with_reverse_order
assert_equal 6, Account.order(:credit_limit).reverse_order.count
end

def test_count_with_where_and_order
assert_equal 1, Account.where(firm_name: '37signals').count
assert_equal 1, Account.where(firm_name: '37signals').order(:firm_name).count
assert_equal 1, Account.where(firm_name: '37signals').order(:firm_name).reverse_order.count
end

def test_should_sum_expression def test_should_sum_expression
# Oracle adapter returns floating point value 636.0 after SUM # Oracle adapter returns floating point value 636.0 after SUM
if current_adapter?(:OracleAdapter) if current_adapter?(:OracleAdapter)
Expand Down

0 comments on commit bbad752

Please sign in to comment.