How do I validate two fields for uniqueness

Ruby on-Rails

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

what about this solution Validate combined values

validates :employee_id, uniqueness: { scope: :area_id }

Solution 2 - Ruby on-Rails

validates_uniqueness_of :employee_id, :scope => :area_id

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjohncView Question on Stackoverflow
Solution 1 - Ruby on-RailsNaveedView Answer on Stackoverflow
Solution 2 - Ruby on-RailsJRLView Answer on Stackoverflow