Rails 跨多列验证唯一性,不区分大小写

发布于 2024-08-20 08:03:03 字数 587 浏览 2 评论 0原文

我有一个包含两个字段的模型,我将其称为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水中月 2024-08-27 08:03:03

您是否尝试过 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文