aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-02-08 23:48:21 +0900
committerGitHub <noreply@github.com>2019-02-08 23:48:21 +0900
commit8cae8dcf5473a53179933eee1f0a36db436bbf1e (patch)
treec1dddeaaad52289f14cadce1e8b4c191e90ade48 /activerecord/lib
parente133573c8195f088ce29fbebabe3428cff9dc20d (diff)
parent07dcd99a5a5b0ccbaa91e0d18e3b7b165fecffb1 (diff)
downloadrails-8cae8dcf5473a53179933eee1f0a36db436bbf1e.tar.gz
rails-8cae8dcf5473a53179933eee1f0a36db436bbf1e.tar.bz2
rails-8cae8dcf5473a53179933eee1f0a36db436bbf1e.zip
Merge pull request #35193 from kamipo/fix_exists_with_distinct_and_offset
Fix `relation.exists?` with giving both `distinct` and `offset`
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb12
1 files changed, 8 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 8f1065c1e7..91cfc4e849 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -307,8 +307,6 @@ module ActiveRecord
return false if !conditions || limit_value == 0
- conditions = sanitize_forbidden_attributes(conditions)
-
if eager_loading?
relation = apply_join_dependency(eager_loading: false)
return relation.exists?(conditions)
@@ -316,7 +314,7 @@ module ActiveRecord
relation = construct_relation_for_exists(conditions)
- skip_query_cache_if_necessary { connection.select_value(relation.arel, "#{name} Exists") } ? true : false
+ skip_query_cache_if_necessary { connection.select_one(relation.arel, "#{name} Exists") } ? true : false
end
# This method is called whenever no records are found with either a single
@@ -354,7 +352,13 @@ module ActiveRecord
end
def construct_relation_for_exists(conditions)
- relation = except(:select, :distinct, :order)._select!(ONE_AS_ONE).limit!(1)
+ conditions = sanitize_forbidden_attributes(conditions)
+
+ if distinct_value && offset_value
+ relation = limit(1)
+ else
+ relation = except(:select, :distinct, :order)._select!(ONE_AS_ONE).limit!(1)
+ end
case conditions
when Array, Hash