Giter VIP home page Giter VIP logo

activerecord-rescue_from_duplicate's Introduction

ActiveRecord - RescueFromDuplicate

Build Status

This gem will rescue SQL errors when trying to insert records that fail uniqueness validation.

It complements :validates_uniqueness_of and will add appropriate errors.

Additionally, a macro allows you to assume that the record will be unique and rescue gracefully otherwise.

Tested with:

  • ActiveRecord: 3.2, 4.0, 4.1, 4.2, edge
  • Ruby 1.9.3, 2.0.0, 2.1.2
  • MySQL, PostgreSQL, Sqlite3

Note:

  • after_validation, before_save, before_create will have been run. Make sure they don't have undesired side-effects.
  • Unlike failed validation, ActiveRecord::RecordNotSaved will be raised when using create!, save! or other ! methods.

Usage

With validation

Add the rescue_from_duplicate: true to any regular uniqueness validation.

This will use the Rails standard validation, performing a SELECT to ensure the record is valid. In the case of a race condition, an error will be added to the model, and no exception will be thrown.

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"]

Without validation

You can use this if you don't need to check that the record is unique before attempting to insert it. It will not add any validation to the model, but it will add an error if the persistance fails.

class ModelWithUniqueToken < ActiveRecord::Base
  rescue_from_duplicate :token, scope: :shop_id
end

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

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.