Skip to content

Instantly share code, notes, and snippets.

@gilr00y
Created June 24, 2014 11:57
Show Gist options
  • Save gilr00y/bce2dbba5b7329b2ef94 to your computer and use it in GitHub Desktop.
Save gilr00y/bce2dbba5b7329b2ef94 to your computer and use it in GitHub Desktop.
ActiveRecord Cross-DB transactions
# AR transactions wrap a block of code, issuing a rollback if any errors are raised,
# returning result of block if no errors.
#(note: this gist makes use of SecondBase https://github.com/customink/secondbase)
AR = ActiveRecord::Base
AR.transaction { 14 } #=> 14
# Errors are propagated...
AR.transaction { raise } #=> RuntimeError
# ...except ActiveRecord::Rollback errors
AR.transaction { raise ActiveRecord::Rollback } #=> nil
# --
# What does this mean cross-db??
# --
# We can nest transactions!
AR.transaction {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment