[英]Ruby on Rails: Getting validates_uniqueness_of to work
I have two params :work and :grade. 我有两个参数:work和:grade。 In the model, before saving I want to use validates_uniqueness_of to check given a unique work, there is only one grade. 在模型中,保存之前,我想使用validates_uniqueness_of检查给定的唯一工作,只有一个等级。 Grade can be the same for other work. 成绩可以与其他作品相同。 How would I write this? 我该怎么写?
Edit: 编辑:
validates_uniqueness_of :work, :scope => :grade validates_uniqueness_of:work,:scope =>:grade
If you have a deprecated syntax warning, you can write it so: 如果您已弃用语法警告,则可以这样编写:
validates :work, uniqueness: {scope: :grade}, presence: true
Edit: 编辑:
It seems you need a two way checking, so perhaps adding this will work: 看来您需要两种方式进行检查,因此也许可以添加以下方法:
validates :grade, uniqueness: {scope: :work}, presence: true
Although under high load I've seen this fail, so best is to create a database constraint. 尽管在高负载下我已经看到了失败,但是最好还是创建一个数据库约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.