how can I make this query not CaSeSeNsITIVe?

how can I make this query not Case sensitive?

  @courses = Course.where('name LIKE ?', "%#{params[:name]}%")

2 Answers

When you use a Postgres database then you might want to consider ILIKE which is the case insensitive version of LIKE:

@courses = Course.where('name ILIKE ?', "%#{params[:name]}%")

A typical solution is to turn both values to lower case before comparing them:

@courses = Course.where('LOWER(name) LIKE LOWER(?)', "%#{params[:name]}%")

Depending on your RDMBS, LOWER() might not be available.

Also depending on your RDBMS, there could be more efficient options available (since using a string function basically defeats an existing index). For example in MySQL, you set a case-insensitive collation on the concerned column, which will make all string comparisons case-insensitive by default, while allowing the use of an index.

Archive from:

Leave a Reply

Your email address will not be published. Required fields are marked *