Escaping values in Rails (similar to mysql_real_escape_string())

后端 未结 6 1826
我在风中等你
我在风中等你 2020-12-14 00:54

I know about prepared statements, but if I\'m using raw SQL, does ActiveRecord have a way to manually escape values?

Something like this would be nice:



        
6条回答
  •  既然无缘
    2020-12-14 01:38

    A quick dive into the ActiveRecord source reveals its method "sanitize_sql_array" for sanitizing the [string, bind_variable[, bind_variable]] type of sql statement

    You could call it directly:

    sql = ActiveRecord::Base.send(:sanitize_sql_array, ["insert into foo (bar, baz) values (?, ?), (?, ?)", 'a', 'b', 'c', 'd'])
    res = ActiveRecord::Base.connection.execute(sql)
    

提交回复
热议问题