Last active
September 9, 2016 19:16
-
-
Save abelorian/2497f9c0e599989a85763d246ebc496e to your computer and use it in GitHub Desktop.
Case insensitive search in mongoid + Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query = 'Ab' | |
User.where(name: query) #Exact match | |
User.where(name: /.*#{query}.*/) # Case sensitive, as LIKE operator | |
User.where(name: /.*#{query}.*/i) # Case sensitive, as iLIKE operator | |
# LIKE OR LIKE | |
User.any_of({first_name: /.*#{query}.*/i}).any_of({ last_name: /.*#{query}.*/i }) | |
# Don`t forget the .to_a method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment