List changed attrs in pessimistic lock error

If `record.lock!` fails because the record has unpersisted changes, it can help
to see which attributes changed.
This commit is contained in:
Petrik 2022-01-20 19:09:19 +01:00
parent 2197814074
commit 3212ae7f6b
2 changed files with 3 additions and 1 deletions

@ -71,6 +71,7 @@ def lock!(lock = true)
Locking a record with unpersisted changes is not supported. Use
`save` to persist the changes, or `reload` to discard them
explicitly.
Changed attributes: #{changed.map(&:inspect).join(', ')}.
MSG
end

@ -706,9 +706,10 @@ def test_lock_does_not_raise_when_the_object_is_not_dirty
def test_lock_raises_when_the_record_is_dirty
person = Person.find 1
person.first_name = "fooman"
assert_raises(RuntimeError) do
error = assert_raises(RuntimeError) do
person.lock!
end
assert_match(/Changed attributes: "first_name"/, error.message)
end
def test_locking_in_after_save_callback