Prev | Current Page 461 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

However, as of Active Record 1.15, the BigDecimal
class is supported for columns of type :decimal. Ruby??™s BigDecimal class provides arbitraryprecision
floating-point arithmetic.
What Database Locking Mechanisms Does Active Record Support?
While Active Record supports its own optimistic and pessimistic record-level locking, there is
no built-in support for table level locking. Fortunately, it is relatively straightforward to add
this functionality. First, create the table locking module:
module TableLocking
def lock_table(options = {})
sql = "LOCK TABLES #{table_name}"
sql += ' WRITE' if options[:write]
sql += ' READ' if options[:read]
execute sql
end
CHAPTER 8 ?–  ACTIVE RECORD AND THE REAL WORLD 206
def unlock_table
execute "UNLOCK TABLES"
end
end
Next, in your Active Record class, include the module:
class User < ActiveRecord::Base
include TableLocking
# your additional model code
end
With the module included in your class, you can now lock and unlock the table at will for
read access, write access, or both, as in the following example:
User.


Pages:
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473