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.
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.
I found this code here: https://stackoverflow.com/a/6987482/2754188
You can use this line:
validates :name, uniqueness: { case_sensitive: false }
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.
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.