rails 3 验证多个属性的唯一性 [英] rails 3 validation on uniqueness on multiple attributes
本文介绍了rails 3 验证多个属性的唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 Rails 3.0.0.beta4
I use Rails 3.0.0.beta4
我想对两个属性的唯一性添加验证,这意味着如果 'recorded_at'
和 'zipcode'
的一对是唯一的,我的模型是有效的.
I want to add a validation on uniqueness on two attributes, that means that my model is valid if the couple of 'recorded_at'
and 'zipcode'
is unique.
这里的一个属性是语法
validates :zipcode, :uniqueness => true
谢谢
推荐答案
在 Rails 2 中,我会写:
In Rails 2, I would have written:
validates_uniqueness_of :zipcode, :scope => :recorded_at
在 Rails 3 中:
In Rails 3:
validates :zipcode, :uniqueness => {:scope => :recorded_at}
对于多个属性:
validates :zipcode, :uniqueness => {:scope => [:recorded_at, :something_else]}
这篇关于rails 3 验证多个属性的唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文