Rails“validates_uniqueness_of"区分大小写 [英] Rails "validates_uniqueness_of" Case Sensitivity

查看:18
本文介绍了Rails“validates_uniqueness_of"区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是模型(我使用的是 SQLLite3):

Here is the model (I am using SQLLite3):

class School < ActiveRecord::Base

  validates_uniqueness_of :name

end

例如,添加Yale"后,我无法添加Yale",但可以添加yale".如何使验证不区分大小写?

For example, after I add "Yale", I cannot add "Yale" but can add "yale." How can I make the validation case insensitive?

找到它 - Active Record Validations

推荐答案

validates_uniqueness_of :name, :case_sensitive =>false 可以解决问题,但您应该记住 validates_uniqueness_of 保证唯一性,如果您有多个服务器/服务器进程(例如运行 PhusionPassenger、多个Mongrels 等)或多线程服务器.那是因为您可能会得到以下事件序列(顺序很重要):

validates_uniqueness_of :name, :case_sensitive => false does the trick, but you should keep in mind that validates_uniqueness_of does not guarantee uniqueness if you have multiple servers/server processes (e.g. running Phusion Passenger, multiple Mongrels, etc) or a multi-threaded server. That's because you might get this sequence of events (the order is important):

  1. 进程 A 收到创建名为foo"的新用户的请求
  2. 进程 B 做同样的事情
  3. 进程 A 通过询问数据库该名称是否存在来验证 'foo' 的唯一性,而数据库表示该名称尚不存在.
  4. 进程 B 做同样的事情并得到同样的响应
  5. 进程 A 提交新记录的 insert 语句并成功
  6. 如果您有一个需要该字段唯一性的数据库约束,进程 B 将提交新记录的 insert 语句,并且 fail 并返回一个丑陋的服务器异常来自 SQL 适配器.如果您没有数据库约束,插入将成功,您现在有两行以 'foo' 作为名称.
  1. Process A gets a request to create a new user with the name 'foo'
  2. Process B does the same thing
  3. Process A validates the uniqueness of 'foo' by asking the DB if that name exists yet and the DB says the name doesn't exist yet.
  4. Process B does the same thing and gets the same response
  5. Process A submits the insert statement for the new record and succeeds
  6. If you have a database constraint requiring uniqueness for that field, Process B will submit the insert statement for the new record and fail with a ugly server exception that comes back from the SQL adapter. If you do not have a database constraint, the insert will succeed and you now have two rows with 'foo' as the name.

另请参阅validates_uniqueness_of<中的并发性和完整性"/code> Rails 文档.

See also "Concurrency and integrity" in the validates_uniqueness_of Rails documentation.

来自 Ruby on Rails 第 3 版:

...尽管它的名字,validates_uniqueness_of 并不能真正保证列值是唯一的.它所能做的就是验证在执行验证时没有任何列具有与正在验证的记录中的值相同的值.可以同时创建两条记录,每条记录都具有相同的列值,该列应该是唯一的,并且两条记录都通过验证.强制唯一性的最可靠方法是使用数据库级约束."

...despite its name, validates_uniqueness_of doesn’t really guarantee that column values will be unique. All it can do is verify that no column has the same value as that in the record being validated at the time the validation is performed. It’s possible for two records to be created at the same time, each with the same value for a column that should be unique, and for both records to pass validation. The most reliable way to enforce uniqueness is with a database-level constraint."

另见 这位程序员的经验validates_uniqueness_of.

这种情况经常发生的一种方式是在创建新帐户时意外从网页上重复提交.这是一个很难解决的问题,因为用户将返回的是第二个(丑陋的)错误,这会让他们认为他们的注册失败了,而实际上它成功了.我发现防止这种情况的最好方法就是使用 javascript 来尝试防止重复提交.

One way this commonly happens is accidental double-submissions from a web page when creating a new account. This is a hard one to solve because what the user will get back is the second (ugly) error and it will make them think their registration failed, when in reality it succeeded. The best way I've found to prevent this is just to use javascript to try to prevent double-submission.

这篇关于Rails“validates_uniqueness_of"区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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