Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I write a case-insensitive find_by_email for Rails 3

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 = [email protected]

params[:user][:email] = [email protected]


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?

like image 622
chell Avatar asked Dec 04 '22 19:12

chell


1 Answers

If a user(say X) has an email '[email protected]', then

User.find(:all, :conditions => ["lower(email) =?", "[email protected]"]) 

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) = ?', "[email protected]").first
like image 185
prasvin Avatar answered Mar 11 '23 19:03

prasvin