aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/finder_methods.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-04-08 22:42:31 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-04-09 02:33:47 +0900
commit0da696a5e3cee87a996a020b664caa1eabd66220 (patch)
tree61433f73a032c04ba559bbd5b4baa22741480d5b /activerecord/lib/active_record/relation/finder_methods.rb
parenta76f5189f6cec4b3e6d9035e2b55dcda6050dfdb (diff)
downloadrails-0da696a5e3cee87a996a020b664caa1eabd66220.tar.gz
rails-0da696a5e3cee87a996a020b664caa1eabd66220.tar.bz2
rails-0da696a5e3cee87a996a020b664caa1eabd66220.zip
Extract `construct_relation_for_exists` in `FinderMethods`
To ease to customize a relation for `exists?`.
Diffstat (limited to 'activerecord/lib/active_record/relation/finder_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb27
1 files changed, 15 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 5d24f5f5ca..14c2ddd85b 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -312,16 +312,7 @@ module ActiveRecord
relation = apply_join_dependency(self, construct_join_dependency(eager_loading: false))
return false if ActiveRecord::NullRelation === relation
- relation = relation.except(:select, :distinct).select(ONE_AS_ONE).limit(1)
-
- case conditions
- when Array, Hash
- relation = relation.where(conditions)
- else
- unless conditions == :none
- relation = relation.where(primary_key => conditions)
- end
- end
+ relation = construct_relation_for_exists(relation, conditions)
connection.select_value(relation, "#{name} Exists", relation.bound_attributes) ? true : false
rescue ::RangeError
@@ -391,6 +382,19 @@ module ActiveRecord
end
end
+ def construct_relation_for_exists(relation, conditions)
+ relation = relation.except(:select, :distinct)._select!(ONE_AS_ONE).limit!(1)
+
+ case conditions
+ when Array, Hash
+ relation.where!(conditions)
+ else
+ relation.where!(primary_key => conditions) unless conditions == :none
+ end
+
+ relation
+ end
+
def construct_join_dependency(joins = [], eager_loading: true)
including = eager_load_values + includes_values
ActiveRecord::Associations::JoinDependency.new(@klass, including, joins, eager_loading: eager_loading)
@@ -401,8 +405,7 @@ module ActiveRecord
end
def apply_join_dependency(relation, join_dependency)
- relation = relation.except(:includes, :eager_load, :preload)
- relation = relation.joins join_dependency
+ relation = relation.except(:includes, :eager_load, :preload).joins!(join_dependency)
if using_limitable_reflections?(join_dependency.reflections)
relation