ActiveRecord - RescueFromDuplicate
This gem will rescue from MySQL and Sqlite errors when trying to insert records that fail uniqueness validation. PostgreSQL is not supported at the moment because of the errors raised when using prepared statements.
It complements :validates_uniqueness_of and will add appropriate errors.
Note:
- All
before_*filters will have been run. - Unlike failed validation,
ActiveRecord::RecordNotSavedwill be raised when usingcreate!,save!or other!methods.
Installation
Add this line to your application's Gemfile:
gem 'activerecord-rescue_from_duplicate'
And then execute:
$ bundle
Or install it yourself as:
$ gem install activerecord-rescue_from_duplicate
Usage
Add the :rescue_from_duplicate => true to any regular uniqueness validation.
class ModelWithUniquenessValidator < ActiveRecord::Base
validates_uniqueness_of :name, :scope => :shop_id, :rescue_from_duplicate => true
end
If two of this statement go in at the same time, and the original validation on uniqueness of name passes, the DBMS will raise an duplicate record error.
a = ModelWithUniquenessValidator.create(:name => "name")
# in a different thread, causing race condition
b = ModelWithUniquenessValidator.create(:name => "name")
a.persisted? #=> true
b.persisted? #=> false
b.errors[:name] #=> ["has already been taken"]
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
