I want validate uniqueness with a given number of repetitions, for example allowed three users with the same name.
User id: 1, name: "user"
User id: 2, name: "user"
User id: 3, name: "user"
But you can not create a fourth user with the same name. What should I do:
- Create validates_uniqueness_of with params?
- Or create custom validation?
I would be grateful for examples.
You’ll need a custom validation, such as
Be aware that database uniqueness checks in your application are prone to race conditions: See Concurrency and Integrity.
EDIT: added new_record? + name_changed? check as updating a record won’t count towards the 3.