0

I have a model with two columns 'name' and 'size'. I am trying to validate the uniqueness of size only if name and size together doesn't exist in database.

For example.

Name = Shirt and Size = L are in database then Name = Shirt and Size = L shouldn't pass the unique validation rather Name = Pant and Size = L should pass the uniqueness.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Santosh Aryal
  • 1,276
  • 1
  • 18
  • 41

2 Answers2

2

if you want to scope with single column then:

validates_uniqueness_of :name, :scope => :size, message: "should be unique"

for multiple columns:

validates :name, uniqueness: {scope: [:size, :attr1, :attr2]}
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
1

Here you can do just like that

validates :name, uniqueness: { scope: :size,
message: "should be unique" }
Tushar Pal
  • 503
  • 6
  • 16