Skip to content

Instantly share code, notes, and snippets.

@crafterm
Forked from benhoskings/atomic.rb
Created February 21, 2010 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crafterm/310585 to your computer and use it in GitHub Desktop.
Save crafterm/310585 to your computer and use it in GitHub Desktop.
module ActiveRecord
class Base
def atomic &block
self.class.transaction {
lock!
yield
}
end
end
end
class Sequence < ActiveRecord::Base
# so instead of this,
def nextval
Sequence.transaction {
lock!
returning self.value += 1 do
save!
end
}
end
# we can do this
def nextval
atomic {
returning self.value += 1 do
save!
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment