Skip to content

Instantly share code, notes, and snippets.

@benhoskings
Created February 21, 2010 10:38
Show Gist options
  • Save benhoskings/310253 to your computer and use it in GitHub Desktop.
Save benhoskings/310253 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