Rails 跨多列验证唯一性,不区分大小写
我有一个包含两个字段的模型,我将其称为first_name 和last_name,并且我想确保两者的组合不区分大小写是唯一的。我已经通过使用这个完成了一半:
validates_uniqueness_of :first_name, :scope => :last_name
问题是唯一性检查似乎区分大小写,即使 文档说默认情况下应该不区分大小写。因此,给定现有记录:
{ :first_name => 'John', :last_name => 'Smith' }
这将被允许:
{ :first_name => 'JOHN', :last_name => 'SMITH' }
以及名字或姓氏中存在大小写变化的任何其他记录。为什么允许这些记录?如何在两个字段之间强制执行不区分大小写的唯一性?
I have a model that has two fields, which I will call first_name and last_name, and I want to make sure that the combination of the two are case-insensitively unique. I've gotten halfway there by using this:
validates_uniqueness_of :first_name, :scope => :last_name
The problem is that the uniqueness check seems to be case sensitive, even though the documentation says it should be case insensitive by default. So given an existing record:
{ :first_name => 'John', :last_name => 'Smith' }
This will be allowed:
{ :first_name => 'JOHN', :last_name => 'SMITH' }
As well as any additional record where there is any variation of case in either the first or last name. Why are these records being allowed? How can I enforce case insensitive uniqueness across both fields together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过 validates_uniqueness_of :first_name, :scope => :姓氏, :区分大小写 =>假的?
文档说明默认情况下这是正确的。
(我认为您提供的链接是一些过时的文档。IIRC,默认值在过去几年中确实发生了变化。)
Did you try
validates_uniqueness_of :first_name, :scope => :last_name, :case_sensitive => false
?The documentation says it's true by default.
(I think the link you gave is to some outdated documentation. IIRC, the default for this did change in the last couple of years.)