Case-insensitive Rails PostgreSQL with LIKE

I have this in the controller:

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

I think this query is case sensitive. This should not be case sensitive.

+7
source share
2 answers

You can use ILIKE instead:

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

From the doc doc:

The ILIKE keyword can be used instead of LIKE to make case insensitive matches according to the active locale. This is not in SQL, but is an extension of PostgreSQL.

+25
source

To use case insensibility when searching with PostgreSQL using LOWER ...

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 
+2
source

All Articles