Escaping values in Rails (similar to mysql_real_escape_string())

后端 未结 6 1856
我在风中等你
我在风中等你 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:34

    Even with Model.find_by_sql you can still use the form where question marks stand in as escaped values.

    Simply pass an array where the first element is the query and succeeding elements are the values to be substituted in.

    Example from the Rails API documentation:

    Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]

提交回复
热议问题