简体   繁体   English

关于Rails中的validates_uniqueness_of

[英]About validates_uniqueness_of in Rails

i have a question, suppose that i have a model called Client, this model have a validation validates_uniqueness_of for attribute name, the problem is that the application is used by multiples users(use Client table), so lets say that a User 1 create a Client called Paul when other user try create a user with same same the validation will work, should however not. 我有一个问题,假设我有一个名为Client的模型,该模型对属性名称进行了验证validates_uniqueness_of,问题是该应用程序被多个用户使用(使用Client表),因此,假设用户1创建了一个当其他用户尝试创建具有相同验证权限的用户时,客户端呼叫Paul,但是不应这样做。

I know if I did understand, 我知道我是否明白

Thanks 谢谢

You can use :scope option: 您可以使用:scope选项:

:scope - One or more columns by which to limit the scope of the uniqueness constraint. :scope-用来限制唯一性约束范围的一列或多列。

class Client < ActiveRecord::Base
  validates_uniqueness_of :name, :scope => :user_id
end

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

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