Rails 4 LIKE 查询 - ActiveRecord 添加引号 [英] Rails 4 LIKE query - ActiveRecord adds quotes
本文介绍了Rails 4 LIKE 查询 - ActiveRecord 添加引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试做一个像这样的查询
I am trying to do a like query like so
def self.search(search, page = 1 )
paginate :per_page => 5, :page => page,
:conditions => ["name LIKE '%?%' OR postal_code like '%?%'", search, search], order => 'name'
end
但是当它运行时,某些东西会添加引号,这会导致 sql 语句像这样出现
But when it is run something is adding quotes which causes the sql statement to come out like so
SELECT COUNT(*)
FROM "schools"
WHERE (name LIKE '%'havard'%' OR postal_code like '%'havard'%')):
所以你可以看到我的问题.我正在使用 Rails 4 和 Postgres 9,这两者我都从未使用过,所以不确定它和 activerecord 的东西还是 postgres 的东西.
So you can see my problem. I am using Rails 4 and Postgres 9 both of which I have never used so not sure if its and an activerecord thing or possibly a postgres thing.
我该如何设置,以便在最终查询中得到 '%my_search%'
?
How can I set this up so I have like '%my_search%'
in the end query?
推荐答案
你的占位符被一个字符串替换,你没有正确处理它.
Your placeholder is replaced by a string and you're not handling it right.
替换
"name LIKE '%?%' OR postal_code LIKE '%?%'", search, search
与
"name LIKE ? OR postal_code LIKE ?", "%#{search}%", "%#{search}%"
这篇关于Rails 4 LIKE 查询 - ActiveRecord 添加引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文