提交 796a43de 编写于 作者: R Rick Olson

Allow ordering of calculated results and/or grouped fields in calculations...

Allow ordering of calculated results and/or grouped fields in calculations [solo@gatelys.com] closes #4228

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3865 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 c6abe81b
*SVN*
* Allow ordering of calculated results and/or grouped fields in calculations [solo@gatelys.com]
* Dynamically set allow_concurrency. #4044 [Stefan Kaes]
* Added Base#to_xml that'll turn the current record into a XML representation [DHH]. Example:
......
......@@ -151,6 +151,7 @@ def construct_calculation_sql(aggregate, aggregate_alias, options)
add_conditions!(sql, options[:conditions])
sql << " GROUP BY #{options[:group_field]}" if options[:group]
sql << " HAVING #{options[:having]}" if options[:group] && options[:having]
sql << " ORDER BY #{options[:order]}" if options[:order]
sql.join
end
......
......@@ -37,6 +37,17 @@ def test_should_group_by_summed_field
assert_equal 60, c[2]
end
def test_should_order_by_grouped_field
c = Account.sum(:credit_limit, :group => :firm_id, :order => "firm_id")
assert_equal [nil, 1, 2, 6], c.keys
end
def test_should_order_by_calculation
c = Account.sum(:credit_limit, :group => :firm_id, :order => "sum_credit_limit desc, firm_id")
assert_equal [105, 60, 50, 50], c.keys.collect { |k| c[k] }
assert_equal [6, 2, nil, 1], c.keys
end
def test_should_group_by_summed_field_having_condition
c = Account.sum(:credit_limit, :group => :firm_id,
:having => 'sum(credit_limit) > 50')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册