开发者

Rails validates_uniqueness_of across multiple columns with case insensitivity

开发者 https://www.devze.com 2022-12-19 04:20 出处:网络
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 usi

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 => 'J开发者_开发知识库ohn', :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?


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号