jissy
jissy

Reputation: 463

validate uniqueness in rails4 and case sensitivity fails

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.

Upvotes: 1

Views: 2780

Answers (3)

Anthony Geiser
Anthony Geiser

Reputation: 121

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

You can use this line:

validates :name, uniqueness: { case_sensitive: false }

Upvotes: 8

Rohan Daxini
Rohan Daxini

Reputation: 494

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.

Upvotes: 1

ernd enson
ernd enson

Reputation: 1772

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.

Upvotes: 1

Related Questions