Skip to content

Instantly share code, notes, and snippets.

@abelorian
Last active September 9, 2016 19:16
Show Gist options
  • Save abelorian/2497f9c0e599989a85763d246ebc496e to your computer and use it in GitHub Desktop.
Save abelorian/2497f9c0e599989a85763d246ebc496e to your computer and use it in GitHub Desktop.
Case insensitive search in mongoid + Rails
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