Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
776 views
in Technique[技术] by (71.8m points)

ruby on rails - How to make LIKE clause case-insensitive?

I'm using a LIKE clause in Ruby On Rails. When I try to search for records by typing "more" it doesn't return anything, but when I do with "More", then it returns the records which contains More keyword, so it seems like it behaves in a case-sensitive way.

Is it possible to make this case-insensitive?

Here is the query I am using currently:

Job.where('title LIKE ? OR duration LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I assume you're using Postgres.

You can use ILIKE

Job.where('title ILIKE ? OR duration ILIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")

Or a some tricky hack lower():

Job.where('lower(title) LIKE lower(?) OR lower(duration) LIKE lower(?)', "%#{params[:search]}%", "%#{params[:search]}%")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...