Skip to content

Instantly share code, notes, and snippets.

View srt32's full-sized avatar
πŸ’­
wahoo

Simon Taranto srt32

πŸ’­
wahoo
  • GitHub
  • WORLD
  • 23:41 (UTC -04:00)
View GitHub Profile
@felipeelias
felipeelias / post-receive.sh
Created October 27, 2011 13:10
Deploy with git's post-receive hook
#!/bin/bash
APP_PATH=/home/applicake/app
# Production environment
export RAILS_ENV="production"
export PATH="/opt/ruby/bin:$PATH"
exit_with_error() {

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example