I'm trying to test the code that I wrote to rescue the
ActiveRecord::StaleObjectError exception...this is the exception that
can occur when a model with optimistic locking is updated by multiple
processes concurrently. For some reason, though, model.reload doesn't
seem to want to actually reload the model from the db when I rescue
the exception.
## i stopped the code here and saw that credit_balance didn't
reflect the
## changes made in the console (see below) even though I checked
via SQL
## and the database DID reflect the update.
retry
end
end
### code executed in the console after credit balance was loaded by
the update_credits method above
c = CreditBalance.find(1) # 1 is the id of the company whose credit
balance
# I'm testing the code above
with
c.update_attribute(:used_credits,999) # I assigned a random number for
used_credits
For some reason this is causing an infinite
loop...credit_balance.reload is not reloading the model from what's in
the database so when it retries it gives the same error. I first
tried this test by updating the value directly via SQL, but that gave
me the same problem so I thought that maybe the way optimistic locking
works it wasn't recognizing the change because it was outside of the
Rails framework.
## i stopped the code here and
## saw that credit_balance didn't
## reflect the changes made in
## the console (see below) even though
## I checked via SQL and the
## database DID reflect the update.
retry
end
end
## code executed in the console
## after credit balance was loaded by
## the update_credits method above.
## 1 is the id of the company whose credit
## balance I'm testing the code above with.
## I updated used_credits with a random num.
c = CreditBalance.find(1)
c.update_attribute(:used_credits,999)
## i stopped the code here and
## saw that credit_balance didn't
## reflect the changes made in
## the console (see below) even though
## I checked via SQL and the
## database DID reflect the update.
I'm not entirely sure if this is what you are asking, but the
before_*, save, after* sequence all runs in one transaction. You
should read up on transaction isolation levels (ie what changes from
other transactions a given transaction sees)