1

validates :name, uniqueness: true

The above validates name with case sensitive uniqueness. Any other default validators/options exists to include to case-insensitive checking.

Please help. Thanks in advance.

ernd enson
  • 1,764
  • 1
  • 14
  • 29
jissy
  • 463
  • 5
  • 20

3 Answers3

8

I found this code here: https://stackoverflow.com/a/6987482/2754188

You can use this line:

validates :name, uniqueness: { case_sensitive: false }
Colto
  • 612
  • 4
  • 14
1

If you're using a text-column, then the following should easily work:

validates_uniqueness_of :name 

The default setting for case_sensitivity is :true and you can even add the following to your validation:

validates_uniqueness_of :name, :case_sensitive => false

This setting is however ignored by non-text columns.

ernd enson
  • 1,764
  • 1
  • 14
  • 29
1

If you are working on uniqueness of a record in Rails app, then please be reminded about this Rails article which says that Rails uniqueness is not fool proof. Scroll down to the bottom of this article Rails - Concurrency and integrity issues to know in detail.

In short, duplicates can still occur during concurrent operations.

I have faced these issue of duplicates in Rails app during concurrency and I had to apply a database level unique index on the table.

Rohan Daxini
  • 496
  • 4
  • 12