Rails 4在不区分大小写的情况下验证电子邮件的唯一性 [英] Rails 4 Validating email uniqueness without case_sensitive

查看:109
本文介绍了Rails 4在不区分大小写的情况下验证电子邮件的唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Ruby 2.1.0,Rails 4.0.2,MySQL和OS X,我无法使验证器uniqueness: { case_insensitive: XXX }正常工作.

Using Ruby 2.1.0, Rails 4.0.2, MySQL, and OS X, I can't get the validator uniqueness: { case_insensitive: XXX } to work right.

是的,我意识到这与

Yes, I realize this is almost, or perhaps precisely, identical to Rails 3. Validating email uniqueness and case sensitive fails. But the answer stream over there goes down a "don't do that" fork, and at least for pedagogic reasons I'd like to actually answer this question. Out of respect for the line taken over there, I've started a new question. Sorry if that's abusive.

我的困难在于,即使(AFAICT)不应, case_sensitive: truecase_sensitive: false也会产生相同的结果.

My difficulty is that case_sensitive: true and case_sensitive: false produce the same results, even when (AFAICT) they oughtn't.

models/user.rb 提取:

class User < ActiveRecord::Base
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@akamai\.com\z/i
  validates :email, presence: true, uniqueness: { case_sensitive: false }, format: {
        with: VALID_EMAIL_REGEX,
        message: "Must be an Akamai email address."
  }
end

spec/models/user_spec.rb 提取:

require 'spec_helper'
describe User do
  before { @user = User.new(email: "giuseppe@akamai.com") }
  subject { @user }
  it { should be_valid }
  context "email" do
    describe "is already taken (caserifically)" do
      before do
        user_with_same_email = @user.dup
        user_with_same_email.email = @user.email.upcase
        user_with_same_email.save
      end
      it { should_not be_valid }
    end
  end
end

按照书面规定,测试通过(user_with_same_email无效).很好.

As written, the test passes (user_with_same_email is not valid). Cool.

但是,如果我将验证的false更改为true,则测试仍会通过(user_with_same_email仍然无效,可能会发生冲突).太冷了.

However, if I change the validation's false to true, the test still passes (user_with_same_email is still invalid, presumably colliding). Uncool.

我知道MySQL/OSX在表名方面是不区分大小写的(由于不区分大小写的基础文件系统),但这不会扩展到值比较吗?

I know that MySQL/OSX is case-insensitive as regards table names (due to the case-insensitive underlying file system), but this doesn't extend to value comparisons, does it?

Ooo,刚刚注意到了(未答复).我相信这些都是重复的.

Ooo, just noticed the (unanswered) Ruby on Rails Tutorial - Michael Hartl - Test "User when email address is already taken". I believe these are exact duplicates.

推荐答案

我不知道您的特定数据库设置,但是是的,我想这很可能是问题所在.

I don't know about your specific database setup, but yes, I would say that is most likely the issue.

这是Rails API指南中对:uniqueness的引用:

This is a quote from the Rails API guide for :uniqueness:

请注意,某些数据库被配置为仍然执行不区分大小写的搜索.

Note that some databases are configured to perform case-insensitive searches anyway.

http://edgeguides.rubyonrails.org/active_record_validations.html#uniqueness

据我所知,目前尚无官方补丁.我能找到的最好的是这些古老的讨论:

As far as I can tell, there isn't an official patch for this. Best I can find are these old discussions:

  • https://github.com/rails/rails/issues/1399
  • https://github.com/rails/rails/issues/613

两者都提到了解决问题的方法.

Both mention various ways around it.

我想说,最好的选择是将它与您的生产环境类似.似乎它在不同平台上的行为有所不同.

I'd say your best bet is running it against what your production environment will be like too. Seems it behaves differently on different platforms.

这篇关于Rails 4在不区分大小写的情况下验证电子邮件的唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆