简体   繁体   English

Ruby on Rails验证唯一性

[英]Ruby on Rails Validation Uniqueness

I am working in my ruby on rails assignment that will let users input their first name, middle name and last name. 我正在使用ruby进行Rails分配,以便用户输入名字,中间名和姓氏。 Now I want to validate each of them using uniqueness. 现在,我想使用唯一性来验证它们。 Of course, if the first, middle and last name already exist, it cannot be created. 当然,如果名字,中间名和姓氏已经存在,则无法创建。 But if the first name is the only one that has and existing match in the database and the middle and last name is different, it should be created. 但是,如果名字是唯一一个在数据库中具有现有匹配项且中间名和姓氏不同的名字,则应创建它。 Another example is that if only the middle name has an existing match in the database and the other two are different. 另一个示例是,如果仅中间名在数据库中具有现有匹配项,而另外两个不同。 The problem is that if I use, validates :first_name, :middle_name, :last_name, uniqueness: true, it will not cover the said conditions. 问题是,如果我使用,请验证:first_name,:middle_name,:last_name,uniqueness:true,它将不会覆盖上述条件。 Please help me 请帮我

What you're looking for is :scope keyword 您正在寻找的是:scope关键字

Try 尝试

validates :first_name, uniqueness: { scope: [:last_name, :middle_name] }

And refer to documentation 并参考文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM