问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/7963477/how-can-i-write-a-case-insensitive-find-by-email-for-rails-3