Rails beginner
Rails beginner

Reputation: 14504

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.

Upvotes: 16

Views: 10913

Answers (2)

vanessasoutoc
vanessasoutoc

Reputation: 21

For using case insensitive in search 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

Upvotes: 2

ScottJShea
ScottJShea

Reputation: 7111

You can use ILIKE in the where instead:

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

From doc:

The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension.

Upvotes: 39

Related Questions