Skip to content

Instantly share code, notes, and snippets.

@gilr00y
Created June 24, 2014 11:57
Show Gist options
  • Save gilr00y/cff872322a6b984058b0 to your computer and use it in GitHub Desktop.
Save gilr00y/cff872322a6b984058b0 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