Here is the model (I am using SQLLite3):
class School < ActiveRecord::Base validates_uniqueness_of :name end
For example, after I add ‘Yale’, I cannot add ‘Yale’ but can add ‘yale.’ How can I make the validation case insensitive?
EDIT: Found it – Active Record Validations
validates_uniqueness_of :name, :case_sensitive => falsedoes the trick, but you should keep in mind thatvalidates_uniqueness_ofdoes not guarantee uniqueness if you have multiple servers/server processes (e.g. running Phusion Passenger, multiple Mongrels, etc) or a multi-threaded server. That’s because you might get this sequence of events (the order is important):insertstatement for the new record and succeedsinsertstatement for the new record and fail with a ugly server exception that comes back from the SQL adapter. If you do not have a database constraint, the insert will succeed and you now have two rows with ‘foo’ as the name.See also ‘Concurrency and integrity’ in the
validates_uniqueness_ofRails documentation.From Ruby on Rails 3rd Edition:
See also this programmer’s experience with
validates_uniqueness_of.One way this commonly happens is accidental double-submissions from a web page when creating a new account. This is a hard one to solve because what the user will get back is the second (ugly) error and it will make them think their registration failed, when in reality it succeeded. The best way I’ve found to prevent this is just to use javascript to try to prevent double-submission.