Method: ActiveRecord::Locking::Pessimistic#with_lock

Defined in:
activerecord/lib/active_record/locking/pessimistic.rb

#with_lock(*args) ⇒ Object

Wraps the passed block in a transaction, reloading the object with a lock before yielding. You can pass the SQL locking clause as an optional argument (see #lock!).

You can also pass options like requires_new:, isolation:, and joinable: to the wrapping transaction (see ActiveRecord::ConnectionAdapters::DatabaseStatements#transaction).



92
93
94
95
96
97
98
99
# File 'activerecord/lib/active_record/locking/pessimistic.rb', line 92

def with_lock(*args)
  transaction_opts = args.extract_options!
  lock = args.present? ? args.first : true
  transaction(**transaction_opts) do
    lock!(lock)
    yield
  end
end