lock!(lock = true) public

Obtain a row lock on this record. Reloads the record to obtain the requested lock. Pass an SQL locking clause to append the end of the SELECT statement or pass true for “FOR UPDATE” (the default, an exclusive row lock). Returns the locked record.

Show source
Register or log in to add new notes.
October 27, 2009 - (>= v1.0.0)
1 thank

Only works within a transaction

Of course, this has to be done in a transaction, like so:

# we've loaded user earlier and did some checks which took some time
# to make sure, updates made meanwhile by other threads don't lead to
# optimistic locking errors here, we do this when finally suspending
User.transaction do
  user.lock!
  user.suspended = 1
  user.save!
end