From 872ddaf290c954972a52c19dec8c0d6a5f726fdd Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 24 Jan 2005 11:57:22 +0000 Subject: Added bind-named arrays for interpolating a group of ids or strings in conditions #528 [bitsweat] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@485 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/base.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 55fe29c8f5..c1cedbcf61 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -289,7 +289,7 @@ module ActiveRecord #:nodoc: if result.size == ids.size result else - raise RecordNotFound, "Couldn't find #{name} with ID in (#{ids_list})#{conditions}" + raise RecordNotFound, "Couldn't find all #{name.pluralize} with IDs (#{ids_list})#{conditions}" end end end @@ -721,7 +721,7 @@ module ActiveRecord #:nodoc: def replace_bind_variables(statement, values) raise_if_bind_arity_mismatch(statement, statement.count('?'), values.size) bound = values.dup - statement.gsub('?') { connection.quote(bound.shift) } + statement.gsub('?') { quote_bound_value(bound.shift) } end def replace_named_bind_variables(statement, bind_vars) @@ -729,13 +729,22 @@ module ActiveRecord #:nodoc: statement.gsub(/:(\w+)/) do match = $1.to_sym if bind_vars.has_key?(match) - connection.quote(bind_vars[match]) + quote_bound_value(bind_vars[match]) else raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}" end end end + def quote_bound_value(value) + case value + when Array + value.map { |v| connection.quote(v) }.join(',') + else + connection.quote(value) + end + end + def raise_if_bind_arity_mismatch(statement, expected, provided) unless expected == provided raise PreparedStatementInvalid, "wrong number of bind variables (#{provided} for #{expected}) in: #{statement}" -- cgit v1.2.3