retryable_record

Retries an operation on an ActiveRecord until no StaleObjectError is being raised.

Build Status Gem Version

Gem | Source | Documentation

Usage

You can use retryable_record in 3 different ways:

Module function

require 'retryable_record'

RetryableRecord.retry(user) do
  user.username = "foo"
  user.save!
end

Kernel import

require 'retryable_record/import'

RetryableRecord(user) do
  user.username = "foo"
  user.save!
end

Module inclusion

require 'retryable_record'

class User < ActiveRecord::Base
  include RetryableRecord
end

user = User.first

user.retryable do
  user.username = "foo"
  user.save!
end

Optimistic locking (lock_version column)

ActiveRecord migration needs to support optimistic locking. See api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.integer :lock_version

      t.timestamps
    end
  end
end

Credits

Inspired by

TODO

Note on Patches/Pull Requests