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 120 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


If the version of the object you are working on is out of date, when you attempt to save
your object, an ActiveRecord::StaleObjectError exception will be raised. It is your responsibility
to handle this error by either rolling back or providing the logic needed to resolve the
possible conflict. Because of this, it is very common to use transactions along with locking.
To further illustrate optimistic locking, it can be used to ensure that the Account records
are not modified by multiple processes, thereby ensuring that your account data remains consistent.
Here??™s our trusty Account model, repeated here for your reference:
class Account < ActiveRecord::Base
def withdraw amount
update_attribute(:balance, balance-amount)
if balance < 0
raise
end
end
def deposit amount
update_attribute(:balance, balance+amount)
end
end
If optimistic locking is enabled on the Account model by including a lock_version column
in your accounts table, the following code will cause an ActiveRecord::StaleObjectError to
be raised:
my_account1 = Account.


Pages:
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
Darmowe Mp3 - Free Download Śmieszne filmy Compare Fantasy Wypadanie włosów