How to make postgresql search method case insensitive?
I just switched from sqlite3 to postgresql-9.4. Earlier when I searched for my "typeahead" method, I got results that were not case sensitive. But now that I've switched, the method is suddenly case sensitive. How can I make a method case insensitive with postgresql?
def typeahead
q = params[:query]
render json: Subject.where('name like ? OR second_name like ? OR keywords like ?', "%#{q}%", "%#{q}%", "%#{q}%")
end
+3
source to share
3 answers
The general way is as follows:
Subject.where('LOWER(name) like ?, "%#{q.downcase}%")
Note that you may need to create a lowercase index if you run into performance issues. See: How to create an index in PostgreSQL in lowercase only?
+2
source to share