Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I validate two fields for uniqueness

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.

How can I do this.

Thanks

like image 471
johnc Avatar asked Oct 27 '09 19:10

johnc


2 Answers

what about this solution Validate combined values

validates :employee_id, uniqueness: { scope: :area_id } 
like image 81
Naveed Avatar answered Sep 30 '22 07:09

Naveed


validates_uniqueness_of :employee_id, :scope => :area_id 
like image 20
JRL Avatar answered Sep 30 '22 06:09

JRL