aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2008-03-17 04:02:34 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2008-03-17 04:02:34 +0000
commit9e4558671c25f14950e21982ea93d5b977e9d2f8 (patch)
tree8612ed411d5030c12fc9f29d23b238b6efda811f /activerecord
parentb132413885cff785851980dc1740a003109214b7 (diff)
downloadrails-9e4558671c25f14950e21982ea93d5b977e9d2f8.tar.gz
rails-9e4558671c25f14950e21982ea93d5b977e9d2f8.tar.bz2
rails-9e4558671c25f14950e21982ea93d5b977e9d2f8.zip
Ensure that you can still do expressions in calculations (closes #11355) [nbugajski/cavelle]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9043 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/calculations.rb3
-rw-r--r--activerecord/test/cases/calculations_test.rb4
2 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index fc61a221a1..c3e7313f6d 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -111,6 +111,7 @@ module ActiveRecord
# Person.average(:age) # SELECT AVG(age) FROM people...
# Person.minimum(:age, :conditions => ['last_name != ?', 'Drake']) # Selects the minimum age for everyone with a last name other than 'Drake'
# Person.minimum(:age, :having => 'min(age) > 17', :group => :last_name) # Selects the minimum age for any family without any minors
+ # Person.sum("2 * age")
def calculate(operation, column_name, options = {})
validate_calculation_options(operation, options)
column_name = options[:select] if options[:select]
@@ -155,7 +156,7 @@ module ActiveRecord
scope = scope(:find)
merged_includes = merge_includes(scope ? scope[:include] : [], options[:include])
aggregate_alias = column_alias_for(operation, column_name)
- column_name = "#{connection.quote_table_name(table_name)}.#{column_name}" unless column_name == "*" || column_name.to_s.include?('.')
+ column_name = "#{connection.quote_table_name(table_name)}.#{column_name}" if column_names.include?(column_name.to_s)
if operation == 'count'
if merged_includes.any?
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 14987183eb..1771065a66 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -256,4 +256,8 @@ class CalculationsTest < ActiveRecord::TestCase
def test_count_with_too_many_parameters_raises
assert_raise(ArgumentError) { Account.count(1, 2, 3) }
end
+
+ def test_should_sum_expression
+ assert_equal "636", Account.sum("2 * credit_limit")
+ end
end