4

I found that if a user registers with an email and use a capital letter in their email when I do the following I get nil.

Example:

username = Dillan@example.com

params[:user][:email] = dillan@example.com


user = User.find_by_email(params[:user][:email])

user.nil?  

=> true

How can I search for an email without the problems of case sensitivity or how an I write a find_by_email that is case insensitive?

chell
  • 7,064
  • 15
  • 67
  • 129

1 Answers1

16

If a user(say X) has an email 'Dillan@example.com', then

User.find(:all, :conditions => ["lower(email) =?", "dillan@example.com"]) 

should return the user X. I didnt replicate the situation but that is what I guess you essentially need to do.

Or, more better, use where instead of find as pointed out by @MrTheWalrus

User.where('lower(email) = ?', "dillan@example.com").first
prasvin
  • 2,959
  • 20
  • 28
  • 4
    I'd go with `User.where('lower(email) = ?', "dillan@example.com").first`, but the key point (using the SQL lower() function) is the same. – MrTheWalrus Nov 01 '11 at 11:55
  • Thanks MrTheWalrus and prasvin. I used your answers and they worked great. – chell Nov 02 '11 at 03:50
  • @MrTheWalrus - I will post that as answer too, if u dont mind. Thanks. I hear using find is deprecated. – prasvin Nov 02 '11 at 05:04