简体   繁体   English

为单个用户验证Ruby on Rails中的唯一项目名称

[英]Validating unique project names in Ruby on Rails for a single user

In my model class Project , I wish to create projects with a name . 在我的模型类Project ,我希望创建一个带有name项目。 I validate the name like this: 我验证这个名字:

  validates :name, :uniqueness => true

I would like the name for the projects to be unique, only in the scope of a User . 我希望项目的name是唯一的,仅在User范围内。 Meaning, a user cannot have multiple projects with the same name, but the projects table may contain multiple projects with the same name, owned by different users. 这意味着,用户不能拥有多个具有相同名称的项目,但是项目表可能包含多个具有相同名称的项目,这些项目由不同的用户拥有。

看到范围的独特性

validates :name, :presence => true, :uniqueness => {:scope => :user_id}

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

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