aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorRoque Pinel <repinel@gmail.com>2015-06-21 15:46:08 -0400
committerRoque Pinel <repinel@gmail.com>2015-06-30 17:36:51 -0500
commitb220c9f9b4cc5b17af40b09046501f3469929a11 (patch)
treea163a761ea0bdacf428b3c7dbe7ecc5fd57a49d3 /activerecord/lib/active_record
parentbc6ac8609cf79f28047c0928b9433e00e6ea1f09 (diff)
downloadrails-b220c9f9b4cc5b17af40b09046501f3469929a11.tar.gz
rails-b220c9f9b4cc5b17af40b09046501f3469929a11.tar.bz2
rails-b220c9f9b4cc5b17af40b09046501f3469929a11.zip
Allow select with Arel and count as well as calculations with Arel
It allows a query like `User.select(:name).count` to be written using Arel as `User.select(User.arel_table[:name]).count`. It exposes the calculations API to accept Arel nodes: `User.count(User.arel_table[:name])`, `User.sum(User.arel_table[:id])`, `Account.average(Account.arel_table[:credit_limit])`, `Account.maximum(Account.arel_table[:credit_limit])` and `Account.minimum(Account.arel_table[:credit_limit])`.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb4
1 files changed, 3 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index df72ba7e9c..3b7f7ca151 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -217,6 +217,8 @@ module ActiveRecord
end
def aggregate_column(column_name)
+ return column_name if Arel::Expressions === column_name
+
if @klass.column_names.include?(column_name.to_s)
Arel::Attribute.new(@klass.unscoped.table, column_name)
else
@@ -366,9 +368,9 @@ module ActiveRecord
end
end
- # TODO: refactor to allow non-string `select_values` (eg. Arel nodes).
def select_for_count
if select_values.present?
+ return select_values.first if select_values.one?
select_values.join(", ")
else
:all