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

拥有回忆 提交于 2019-12-07 08:27:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!