optimistic.rb 869 B

123456789101112131415161718192021222324252627282930313233343536
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. $: << File.dirname(__FILE__)
  10. require "connect"
  11. require "rubygems"
  12. require "active_record"
  13. ActiveRecord::Schema.define do
  14. create_table :counters, :force => true do |t|
  15. t.integer :count
  16. t.integer :lock_version, :default => 0
  17. end
  18. end
  19. class Counter < ActiveRecord::Base
  20. end
  21. Counter.delete_all
  22. Counter.create(:count => 0)
  23. count1 = Counter.find(:first)
  24. count2 = Counter.find(:first)
  25. count1.count += 3
  26. count1.save
  27. count2.count += 4
  28. count2.save