我想要做像这样类似的查询
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和9的Postgres这两个我从来没有使用过所以不知道如果和一个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}%"