怎么办阿雷尔和Rails一个LIKE查询? [英] How to do a LIKE query in Arel and Rails?
本文介绍了怎么办阿雷尔和Rails一个LIKE查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的事情是这样的:
I want to do something like:
SELECT * FROM USER WHERE NAME LIKE '%Smith%';
我在阿雷尔的尝试:
My attempt in Arel:
# params[:query] = 'Smith'
User.where("name like '%?%'", params[:query]).to_sql
然而,这变为:
However, this becomes:
SELECT * FROM USER WHERE NAME LIKE '%'Smith'%';
阿雷尔包裹的查询字符串'Smith的正确,而是因为这是一个LIKE语句它不工作。
Arel wraps the query string 'Smith' correctly, but because this is a LIKE statement it doesnt work.
怎样才能做一个LIKE查询阿雷尔?
How does one do a LIKE query in Arel?
P.S。奖金 - 我其实是想扫描在桌子上两个字段,这两个名称和描述,来看看是否有任何匹配的查询。如何将这项工作?
P.S. Bonus--I am actually trying to scan two fields on the table, both name and description, to see if there are any matches to the query. How would that work?
推荐答案
这是你如何执行AREL类似的查询:
This is how you perform a like query in arel:
users = User.arel_table
User.where(users[:name].matches("%#{user_name}%"))
PS:
users = User.arel_table
query_string = "%#{params[query]}%"
User.where(users[:name].matches(query_string)\
.or(users[:description].matches(query_string)))
这篇关于怎么办阿雷尔和Rails一个LIKE查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文