LoginSignup
2
2

More than 5 years have passed since last update.

ActiveRecordのwith_lockは自動でreloadされる

Posted at

表題の通り

u = User.last
u.updated_at = nil #=> (1)
u.with_lock do
  u.updated_at #=> (1) では無くDBの値になる
end

該当するRailsのソースはここ

pessimistic.rb
module ActiveRecord
  module Locking
    module Pessimistic
      def lock!(lock = true)
        reload(:lock => lock) if persisted?
        self
      end

      def with_lock(lock = true)
        transaction do
          lock!(lock)
          yield
        end
      end
    end
  end
end
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2