SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 121 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

find 3
my_account2 = Account.find 3
my_account1.withdraw 20
my_account1.save
my_account2.withdraw 40
my_account2.save # This line raises an ActiveRecord::StaleObjectError
The exception was raised, because the object was previously modified. This example is
somewhat contrived, but hopefully, you can envision that the modification of the Account
object might take place in another process or web request, and in that case, knowing that your
underlying record has changed before attempting to save it would be very important.
CHAPTER 2 ?–  ACTIVE RECORD AND SQL 41
Pessimistic Locking
The second formof locking that Active Record is capable of is pessimistic locking. Active
Record??™s implementation of pessimistic locking is simply row-level locking as supported by
your database. Therefore, if your underlying database does not support locking, neither will
Active Record.
There are two different ways to invoke a row-level lock on your data. The first is by using
the :lock option of the find method. As described when we introduced the find method, the
:lock option to find takes either the Boolean true or a string that is a database-specific locking
statement.


Pages:
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133