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:
* .where
* Scopes
* ActiveRecord case insensitive search
* Joins

Table Of Contents
3 Views
12 ActiveRecord Query Interface
19 Caching
23 Rails 5
25 Mongoid
26 Gems
55 CSV