Module: ActiveRecord::BatchTouchingAbstractAdapter

Included in:
ConnectionAdapters::AbstractAdapter
Defined in:
lib/activerecord/batch_touching.rb

Instance Method Summary collapse

Instance Method Details

#transaction(requires_new: nil, isolation: nil, joinable: true, &block) ⇒ Object

Batches up touch calls for the duration of a transaction. after_touch callbacks are also delayed until the transaction is committed.

Examples

# Touches Person.first and Person.last in a single database round-trip.
Person.transaction do
  Person.first.touch
  Person.last.touch
end

# Touches Person.first once, not twice, right before the transaction is committed.
Person.transaction do
  Person.first.touch
  Person.first.touch
end


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/activerecord/batch_touching.rb', line 24

def transaction(requires_new: nil, isolation: nil, joinable: true, &block)
  result = super(requires_new:, isolation:, joinable:) do
    BatchTouching.start(requires_new:, &block)
  end

  if BatchTouching.should_apply_touches?
    super() do
      BatchTouching.apply_touches
    end
  end

  result
end