简体   繁体   English

Rails PostgreSQL使用LIKE进行非区分大小写的搜索

[英]Rails PostgreSQL non-case sensitive search with LIKE

I have this in my controller: 我在我的控制器中有这个:

Konkurrencer.where("title LIKE ?", "%#{params[:q]}%").limit(4)

I think this query is case sensitive. 我认为这个查询区分大小写。 It should not be case sensitive. 它不应该区分大小写。

You can use ILIKE in the where instead: 您可以在其中使用ILIKE

Konkurrencer.where("title ILIKE ?", "%#{params[:q]}%").limit(4)

From doc: 来自doc:

The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. 可以使用关键字ILIKE而不是LIKE来根据活动区域设置使匹配不区分大小写。 This is not in the SQL standard but is a PostgreSQL extension. 这不是SQL标准,而是PostgreSQL扩展。

For using case insensitive in search with PostgreSQL using LOWER... 使用LOWER在PostgreSQL中使用不区分大小写的...

Example: 例:

def self.search(client, date_start, date_end)
       joins(:customer).where("LOWER(customers.name) LIKE ? AND date >= ? AND date <= ?", "%#{client}%", date_start, date_end)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM