如何删除变量周围的引号,以便我可以在 rails 中的 find_by_sql 中使用 LIKE 运算符?
@entries1 = Entry.find_by_sql(["SELECT `entries`.name as name FROM `entries` where `entries`.name like '%?%'",@something])
会产生
SELECT `entries`.name as name FROM `entries` where `entries`.name like '%'hello'%'
当 @something = 'hello'
最佳答案
不要将百分号放在 SQL 中,而是将它们添加到您要注入(inject)的变量中(在转义已经存在的百分号/下划线之后!)
"%#{@something.gsub('%', '\%').gsub('_', '\_')}%"
关于mysql - Rails & MySQL - 如何在 rails 中的 find_by_sql 中使用 LIKE 运算符和 %?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13559263/