assert_equal Ivaldo.last.id, 1
ActiveRecord::Base.transaction do
puts Ivaldo.create!.id
# 1
raise ActiveRecord::Rollback
end
puts Ivaldo.create!.id
# 2
[8] pry(main)> User.order('id desc').find_by(name: 'sam').id
User Load (7.6ms) SELECT "users".* FROM "users" WHERE "users"."name" = 'sam' ORDER BY id desc LIMIT 1
=> 25527
[9] pry(main)> User.order('id').find_by(name: 'sam').id
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."name" = 'sam' ORDER BY id LIMIT 1
=> 2498
[10] pry(main)> User.find_by(name: 'sam').id
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."name" = 'sam' LIMIT 1
=> 9931
sleep 0.001
assert(elapsed < 1)
sleep 0.001
assert(elapsed < 1)
class Frog
cattr_accessor :total_jumps
attr_accessor :jumps
def jump
Frog.total_jumps = (Frog.total_jumps || 0) + 1
self.jumps = (self.jumps || 0) + 1
end
end
def test_global_tracking
assert(Frog.total_jumps.nil?)
end
def test_jumpy
frog = Frog.new
frog.jump
assert(frog.jumps == 1)
end