aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorPaul Nikitochkin <paul.nikitochkin@gmail.com>2013-10-12 14:38:37 +0300
committerPaul Nikitochkin <paul.nikitochkin@gmail.com>2013-10-13 22:13:50 +0300
commitbc293ff690e5478b99a55594f9fa8fe0e709941b (patch)
tree3f059f76dedbf144139369e9b735091909e112cb /activerecord/lib
parent365110196afcf952bc22729d4467d579b708328f (diff)
downloadrails-bc293ff690e5478b99a55594f9fa8fe0e709941b.tar.gz
rails-bc293ff690e5478b99a55594f9fa8fe0e709941b.tar.bz2
rails-bc293ff690e5478b99a55594f9fa8fe0e709941b.zip
Generate subquery for Relation passed as array condition for where
Instead of executing 2 queries for fetching records filtered by array condition with Relation, added generation of subquery to current query. This behaviour will be consistent when passes Relation as hash condition to where Closes: #12415
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/sanitization.rb14
1 files changed, 12 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb
index 0b87ab9926..af4ea0efec 100644
--- a/activerecord/lib/active_record/sanitization.rb
+++ b/activerecord/lib/active_record/sanitization.rb
@@ -127,7 +127,17 @@ module ActiveRecord
raise_if_bind_arity_mismatch(statement, statement.count('?'), values.size)
bound = values.dup
c = connection
- statement.gsub('?') { quote_bound_value(bound.shift, c) }
+ statement.gsub('?') do
+ replace_bind_variable(bound.shift, c)
+ end
+ end
+
+ def replace_bind_variable(value, c = connection)
+ if ActiveRecord::Relation === value
+ value.to_sql
+ else
+ quote_bound_value(value, c)
+ end
end
def replace_named_bind_variables(statement, bind_vars) #:nodoc:
@@ -135,7 +145,7 @@ module ActiveRecord
if $1 == ':' # skip postgresql casts
$& # return the whole match
elsif bind_vars.include?(match = $2.to_sym)
- quote_bound_value(bind_vars[match])
+ replace_bind_variable(bind_vars[match])
else
raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}"
end