diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index be8b672fa7..8bd2a4530b 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1501,7 +1501,9 @@ module ActiveRecord #:nodoc: # Interpolate custom sql string in instance context. # Optional record argument is meant for custom insert_sql. def interpolate_sql(sql, record = nil) - instance_eval("%(#{sql})") + # Parens in the sql in, e.g., subselects, cause a parse error, so + # escape them. + instance_eval("%@#{sql.gsub('@', '\@')}@") end # Initializes the attributes array with keys matching the columns from the linked table and diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index c7e795c0e6..9b5ce8a8ce 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -1018,6 +1018,12 @@ class BasicsTest < Test::Unit::TestCase assert_equal firm.clients.collect{ |x| x.name }.sort, clients.collect{ |x| x.name }.sort end + def test_interpolate_sql + assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo@bar') } + assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar) baz') } + assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar} baz') } + end + private def assert_readers(model, exceptions)