Ruby-on-rails Rails“validates_uniqueness_of”区分大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/690664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:04:31  来源:igfitidea点击:

Rails "validates_uniqueness_of" Case Sensitivity

ruby-on-railsvalidationmodel

提问by GeekJock

Here is the model (I am using SQLLite3):

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

class School < ActiveRecord::Base

  validates_uniqueness_of :name

end

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

比如我加了“耶鲁”后,就不能加“耶鲁”,但可以加“耶鲁”。如何使验证不区分大小写?

EDIT: Found it - Active Record Validations

编辑:找到它 -活动记录验证

回答by Jordan Brough

validates_uniqueness_of :name, :case_sensitive => falsedoes the trick, but you should keep in mind that validates_uniqueness_ofdoes notguarantee 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):

validates_uniqueness_of :name, :case_sensitive => false有诀窍,但您应该记住,如果您有多个服务器/服务器进程(例如运行 Phusion 乘客、多个 Mongrel 等)或多线程服务器,validates_uniqueness_of不能保证唯一性。那是因为您可能会得到以下事件序列(顺序很重要):

  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 insertstatement for the new record and succeeds
  6. If you have a database constraint requiring uniqueness for that field, Process B will submit the insertstatement for the new record and failwith 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.
  1. 进程 A 收到一个创建名为 'foo' 的新用户的请求
  2. 进程 B 做同样的事情
  3. 进程 A 通过询问数据库该名称是否存在来验证 'foo' 的唯一性,并且数据库表示该名称尚不存在。
  4. 进程 B 做同样的事情并得到同样的响应
  5. 进程A提交insert新记录的语句并成功
  6. 如果您有一个需要该字段唯一性的数据库约束,进程 B 将提交insert新记录的语句并失败,并从 SQL 适配器返回一个丑陋的服务器异常。如果您没有数据库约束,插入将成功,您现在有两行以 'foo' 作为名称。

See also "Concurrency and integrity" in the validates_uniqueness_ofRails documentation.

另请参阅validates_uniqueness_ofRails 文档中的“并发性和完整性” 。

From Ruby on Rails 3rd Edition:

Ruby on Rails 第 3 版开始

...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 并不能真正保证列值将是唯一的。它所能做的就是验证在执行验证时没有任何列具有与正在验证的记录中的值相同的值。可以同时创建两条记录,每条记录都具有相同的列值,该列应该是唯一的,并且两条记录都通过验证。强制唯一性的最可靠方法是使用数据库级约束。”

See also this programmer's experiencewith validates_uniqueness_of.

又见这个程序员的经验validates_uniqueness_of

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.

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

回答by MaximusDominus

In rails 3 you can do this in your model:

在 rails 3 中,您可以在模型中执行此操作:

validates :name, :uniqueness => true

or without case_sensitivity

或不区分大小写

validates :name, :uniqueness => {:case_sensitive => false}

回答by vrish88

There is an option where you can specify case insensitivity

有一个选项可以指定不区分大小写

  validates_uniqueness_of :name, :case_sensitive => false

回答by Victor S

There is a similar question but the answer is more interesting: https://stackoverflow.com/a/6422771

有一个类似的问题,但答案更有趣:https: //stackoverflow.com/a/6422771

Basically, using :case_sensitive => falseperforms a very inefficient database query.

基本上, using:case_sensitive => false执行非常低效的数据库查询。