Ruby on Rails - activerecord case insensitive search in ruby on rails- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
- If we need to search an ActiveRecord model for similar values, we 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.
Example
We 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%"))
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Arel will apply the appropriate LIKE or ILIKE construct for the database engine configured.