aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorjvillarejo <arzivian87@gmail.com>2019-02-21 17:55:21 -0300
committerjvillarejo <arzivian87@gmail.com>2019-02-26 12:21:14 -0300
commitfa2c61fc636f958c274692f2a0f3062859797790 (patch)
treea54c83f40d283d735f57a93e460ff532d41dbf74 /activerecord/lib
parent588f97d76fabdf46211150f991c58d48e9fbf0ac (diff)
downloadrails-fa2c61fc636f958c274692f2a0f3062859797790.tar.gz
rails-fa2c61fc636f958c274692f2a0f3062859797790.tar.bz2
rails-fa2c61fc636f958c274692f2a0f3062859797790.zip
fixes different `count` calculation when using `size` manual `select` with DISTINCT
When using `select` with `'DISTINCT( ... )'` if you use method `size` on a non loaded relation it overrides the column selected by passing `:all` so it returns different value than count. This fixes #35214
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb11
1 files changed, 8 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index bdd3c540bb..4f9ddf302e 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -221,7 +221,6 @@ module ActiveRecord
end
private
-
def has_include?(column_name)
eager_loading? || (includes_values.present? && column_name && column_name != :all)
end
@@ -236,10 +235,12 @@ module ActiveRecord
if operation == "count"
column_name ||= select_for_count
if column_name == :all
- if distinct && (group_values.any? || select_values.empty? && order_values.empty?)
+ if !distinct
+ distinct = distinct_select?(select_for_count) if group_values.empty?
+ elsif group_values.any? || select_values.empty? && order_values.empty?
column_name = primary_key
end
- elsif column_name.is_a?(::String) && /\bDISTINCT[\s(]/i.match?(column_name)
+ elsif distinct_select?(column_name)
distinct = nil
end
end
@@ -251,6 +252,10 @@ module ActiveRecord
end
end
+ def distinct_select?(column_name)
+ column_name.is_a?(::String) && /\bDISTINCT[\s(]/i.match?(column_name)
+ end
+
def aggregate_column(column_name)
return column_name if Arel::Expressions === column_name