aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/calculations.rb
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2013-05-01 11:33:11 -0700
committerGodfrey Chan <godfreykfc@gmail.com>2013-05-01 16:36:01 -0700
commit54122067acaad39b277a5363c6d11d6804c7bf6b (patch)
treea6ef77805f609d5c8868602a095f49522034dd71 /activerecord/lib/active_record/relation/calculations.rb
parent09ac1776abc0d3482f491f2d49f47bcb3d9a4ad7 (diff)
downloadrails-54122067acaad39b277a5363c6d11d6804c7bf6b.tar.gz
rails-54122067acaad39b277a5363c6d11d6804c7bf6b.tar.bz2
rails-54122067acaad39b277a5363c6d11d6804c7bf6b.zip
Handle aliased attributes in ActiveRecord::Relation.
When using symbol keys, ActiveRecord will now translate aliased attribute names to the actual column name used in the database: With the model class Topic alias_attribute :heading, :title end The call Topic.where(heading: 'The First Topic') should yield the same result as Topic.where(title: 'The First Topic') This also applies to ActiveRecord::Relation::Calculations calls such as `Model.sum(:aliased)` and `Model.pluck(:aliased)`. This will not work with SQL fragment strings like `Model.sum('DISTINCT aliased')`. Github #7839 *Godfrey Chan*
Diffstat (limited to 'activerecord/lib/active_record/relation/calculations.rb')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb26
1 files changed, 18 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index 64e1ff9a6a..7239270c4d 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -27,7 +27,7 @@ module ActiveRecord
# Calculates the average value on a given column. Returns +nil+ if there's
# no row. See +calculate+ for examples with options.
#
- # Person.average('age') # => 35.8
+ # Person.average(:age) # => 35.8
def average(column_name, options = {})
calculate(:average, column_name, options)
end
@@ -36,7 +36,7 @@ module ActiveRecord
# with the same data type of the column, or +nil+ if there's no row. See
# +calculate+ for examples with options.
#
- # Person.minimum('age') # => 7
+ # Person.minimum(:age) # => 7
def minimum(column_name, options = {})
calculate(:minimum, column_name, options)
end
@@ -45,7 +45,7 @@ module ActiveRecord
# with the same data type of the column, or +nil+ if there's no row. See
# +calculate+ for examples with options.
#
- # Person.maximum('age') # => 93
+ # Person.maximum(:age) # => 93
def maximum(column_name, options = {})
calculate(:maximum, column_name, options)
end
@@ -54,7 +54,7 @@ module ActiveRecord
# with the same data type of the column, 0 if there's no row. See
# +calculate+ for examples with options.
#
- # Person.sum('age') # => 4562
+ # Person.sum(:age) # => 4562
def sum(*args)
if block_given?
ActiveSupport::Deprecation.warn(
@@ -101,6 +101,10 @@ module ActiveRecord
def calculate(operation, column_name, options = {})
relation = with_default_scope
+ if column_name.is_a?(Symbol) && attribute_aliases.key?(column_name.to_s)
+ column_name = attribute_aliases[column_name.to_s].to_sym
+ end
+
if relation.equal?(self)
if has_include?(column_name)
construct_relation_for_association_calculations.calculate(operation, column_name, options)
@@ -149,11 +153,17 @@ module ActiveRecord
#
def pluck(*column_names)
column_names.map! do |column_name|
- if column_name.is_a?(Symbol) && self.column_names.include?(column_name.to_s)
- "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
- else
- column_name
+ if column_name.is_a?(Symbol)
+ if attribute_aliases.key?(column_name.to_s)
+ column_name = attribute_aliases[column_name.to_s].to_sym
+ end
+
+ if self.columns_hash.key?(column_name.to_s)
+ column_name = "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
+ end
end
+
+ column_name
end
if has_include?(column_names.first)