Postgres case insensitive searching with Rails
Case insensitive searching in Postgres:
- use
ilike
instead oflike
(case-insensitive like) - if you want to use =, make both sides either UPPER or LOWER
There are many relatively common things that ActiveRecord doesn't handle, and LIKE matching is one of them. Here is how to achieve this using straight Arel instead.
Model.where( Model.arel_table[:title].matches("%#{search_term}%"))
You can install Arel-Helpers to make this a little easier
Model.where(Model[:title].matches("%#{search_term}%"))
I previously recommended Squeel for this, but the original creator has stopped supporting it and there doesn't seem to be a full-time developer maintaining it. And since it plays around with private ActiveRecord APIs, it needs constant tending.