ActiveRecord case insensitive search

suggest change

If you need to search an ActiveRecord model for similar values, you might be tempted to use LIKE or ILIKE but this isn’t portable between database engines. Similarly, resorting to always downcasing or upcasing can create performance issues.

You can use ActiveRecord’s underlying Arel matches method to do this in a safe way:

addresses = Address.arel_table
Address.where(addresses[:address].matches("%street%"))

Arel will apply the appropriate LIKE or ILIKE construct for the database engine configured.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


ActiveRecord Query Interface:
*ActiveRecord case insensitive search

Table Of Contents
12ActiveRecord Query Interface
55CSV