6

I have to look for all emails from the User model that have the substring "cpg". So it would match "[email protected]", "[email protected]", etc

I'm pretty sure how to do case insensitive (using User.where("lower(email)...") but I don't know how to find a substring.

I'm using Rails 3 + Postgres 9.x

4 Answers 4

15

Nothing in Rails to do it, you can just use PostgreSQL's ilike (case insensitive like function).

Like so: User.where("email ilike '%cpg%'")

1
10

If you don't want to have to always remember to call lower(email) (and input.downcase on your input in Ruby) whenever you search on your email field, you can make the email column itself case insensitive by using the citext data type.

That's what I just did. I created a migration like this:

class ChangeUsersEmailToCitext < ActiveRecord::Migration
  def up
    # Rails 4:
    #enable_extension("citext")
    # Rails 3:
    execute 'create extension citext'

    change_table :users do |t|
      t.change :email, :citext
    end
  end
end

And now I no longer have to do any extra effort to make the queries case insensitive! It's all handled automatically for me behind the scenes now.

This uses PostgreSQL's citext extension.

http://www.sfcgeorge.co.uk/posts/2013/11/12/case-insensitive-usernames-with-postgres has a nice article about this.

3
  • Isn't PostgreSQL great? :) The only thing I've wished for since doing this is that it had the ability to preserve the case as the user entered it, for display purposes ([email protected], for example), but still allow searching to be case-insensitive. I guess I'd need 2 columns to accomplish that...
    – Tyler Rick
    Jun 8, 2015 at 20:44
  • It works for me. Are you sure you're not calling downcase before you enter it?
    – AJcodez
    Jun 8, 2015 at 21:20
  • Not that I know of, but I must have some kind of before filter or something that's doing that automatically, because when I test it on the dbconsole, I see that you are right... update users set email = '[email protected]' where id = 1; select email from users where id = 1; Thanks, I learned something new!
    – Tyler Rick
    Jun 10, 2015 at 23:29
6

Actually, you can do this in Rails agnostically: http://robb.weblaws.org/2013/12/05/yes-rails-supports-case-insensitive-database-queries/

4
  • 1
    This should be the accepted answer - works without code specifically tied to a particular database engine. Aug 7, 2014 at 14:57
  • 1
    First example doesn't use the squeel gem! May 27, 2015 at 19:22
  • 1
    There's a reason Arel isn't well documented - it's an internal Rails API that is not recommended to be used by third-party apps. Aug 29, 2015 at 7:42
  • Link is dead now, sadly
    – DTrejo
    Jan 5, 2023 at 3:52
2

Option 1:

User.where('LOWER(email) = LOWER(?)', search_email)

Option 2: Use Postgres' citex extension which will do it for you (as answered above).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.