Tatamatugas
Tatamatugas

Reputation: 165

Ruby on Rails Validation Uniqueness

I am working in my ruby on rails assignment that will let users input their first name, middle name and last name. Now I want to validate each of them using uniqueness. Of course, if the first, middle and last name already exist, it cannot be created. But if the first name is the only one that has and existing match in the database and the middle and last name is different, it should be created. Another example is that if only the middle name has an existing match in the database and the other two are different. The problem is that if I use, validates :first_name, :middle_name, :last_name, uniqueness: true, it will not cover the said conditions. Please help me

Upvotes: 0

Views: 456

Answers (1)

Nikita Misharin
Nikita Misharin

Reputation: 2020

What you're looking for is :scope keyword

Try

validates :first_name, uniqueness: { scope: [:last_name, :middle_name] }

And refer to documentation

Upvotes: 2

Related Questions