Ruby-on-rails 如何验证两个字段的唯一性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1633297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I validate two fields for uniqueness
提问by johnc
I need to validate the uniqueness of two fields in an object (row) before I add them. Employee_id and area_id are the two fields in my emp_area table. There can be multiple records with the same employee_id and multiple records with the same area_id, but no two records can have the same employee_id and the same area_id. This is sort of like two fields making up a primary-key or unique-key.
在添加它们之前,我需要验证对象(行)中两个字段的唯一性。Employee_id 和 area_id 是我的 emp_area 表中的两个字段。可以有多个具有相同employee_id 的记录和具有相同area_id 的多个记录,但没有两个记录可以具有相同的employee_id 和相同的area_id。这有点像组成主键或唯一键的两个字段。
How can I do this.
我怎样才能做到这一点。
Thanks
谢谢
回答by Naveed
what about this solution Validate combined values
这个解决方案怎么样验证组合值
validates :employee_id, uniqueness: { scope: :area_id }
回答by JRL
validates_uniqueness_of :employee_id, :scope => :area_id

