怎么办阿雷尔和Rails一个LIKE查询?雷尔、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...

我想做的事情是这样的:

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)))
阅读全文

相关推荐

最新文章