如何为 Rails 3 编写不区分大小写的 find_by_email

发布于 2024-12-13 04:50:33 字数 552 浏览 3 评论 0原文

我发现,如果用户使用电子邮件注册并在电子邮件中使用大写字母,那么当我执行以下操作时,我会得到零。

示例:

username = [email protected]

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


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

user.nil?  

=> true

如何在不区分大小写问题的情况下搜索电子邮件,或者如何编写不区分大小写的 find_by_email?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2024-12-20 04:50:33

如果用户(比如 X)的电子邮件地址为“[电子邮件受保护]” ,然后

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

应该返回用户 X。我没有复制这种情况,但我想这就是你本质上需要做的。

或者,更好的是,使用 where 而不是 find 正如 @MrTheWalrus 指出的那样

User.where('lower(email) = ?', "[email protected]").first

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文