我尝试使用以下方法找出您遇到的问题:
class Person < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true
validates_uniqueness_of :name, :case_sensitive => false, :scope => [ :email ]
validates_uniqueness_of :email, :case_sensitive => false, :scope => [ :name ]
end
和
require 'spec_helper'
describe Person do
context 'with duplicate name and email' do
before do
@person1 = Person.new(name: 'test@example.com', email: 'TEST@EXAMPLE.COM')
end
subject { @person1 }
it { should be_valid }
describe '(I) for case-sensitive match of both' do
before do
person = @person1.dup
person.save
end
it { should_not be_valid }
end
describe '(II) for case-insensitive match of name' do
before do
person = @person1.dup
person.name.swapcase!
person.save
end
it { should_not be_valid }
end
describe '(III) for case-insensitive match of email' do
before do
person = @person1.dup
person.email.swapcase!
person.save
end
it { should_not be_valid }
end
describe '(IV) for case-insensitive match of both' do
before do
person = @person1.dup
person.name.swapcase!
person.email.swapcase!
person.save
end
it { should_not be_valid }
end
end
end
从我的示例案例的日志中可以看出,case_sensitive 的行为有点奇怪:
(0.4ms) SAVEPOINT active_record_1
Person Exists (1.2ms) SELECT 1 AS one FROM "people" WHERE (LOWER("people"."name") = LOWER('TEST@EXAMPLE.COM') AND "people"."email" = 'test@example.com') LIMIT 1
Person Exists (0.5ms) SELECT 1 AS one FROM "people" WHERE (LOWER("people"."email") = LOWER('test@example.com') AND "people"."name" = 'TEST@EXAMPLE.COM') LIMIT 1
SQL (3.7ms) INSERT INTO "people" ("created_at", "email", "name", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Sat, 07 Sep 2013 14:21:05 UTC +00:00], ["email", "test@example.com"], ["name", "TEST@EXAMPLE.COM"], ["updated_at", Sat, 07 Sep 2013 14:21:05 UTC +00:00]]
(0.2ms) RELEASE SAVEPOINT active_record_1
Person Exists (0.3ms) SELECT 1 AS one FROM "people" WHERE (LOWER("people"."name") = LOWER('test@example.com') AND "people"."email" = 'TEST@EXAMPLE.COM') LIMIT 1
Person Exists (0.3ms) SELECT 1 AS one FROM "people" WHERE (LOWER("people"."email") = LOWER('TEST@EXAMPLE.COM') AND "people"."name" = 'test@example.com') LIMIT 1
(0.2ms) ROLLBACK
问题似乎是“LOWER”语句仅用于电子邮件或姓名,但不能同时用于两者。基本上,我希望您的代码能够正常工作。
但是,正如在 db-log 中看到的并且还在另一个问题中指出的那样(Rails 3. Validating email uniqueness and casesensitive failed),当性能成为问题时,确保约束中不区分大小写的行为可能不是一个好主意; -) 而是使用 before 过滤器以小写形式保存电子邮件/名称。由于这也可能不是最好的主意(因为您很可能不想丢失名称的大小写信息),您可以使用另一个小写名称-col 来确保约束或相应地使用 after_valition 过滤器。
使用以下模型应该使您的测试套件变得绿色:
class Person < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true
before_validation :downcase_name_email
validates_uniqueness_of :name, :case_sensitive => false, :scope => [ :email ]
validates_uniqueness_of :email, :case_sensitive => false, :scope => [ :name ]
private
def downcase_name_email
self.email = self.email.downcase if self.email.present?
self.name = self.name.downcase if self.name.present?
end
end
最好的,本。
PS:我你要采用小写的方法,一定要迁移你的数据库数据:
Person.update_all('email = LOWER(email)')
Person.update_all('name = LOWER(name)')