Skip to content

Instantly share code, notes, and snippets.

@coalwater
Last active August 29, 2015 14:16
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 coalwater/4fe828c7221c8c327219 to your computer and use it in GitHub Desktop.
Save coalwater/4fe828c7221c8c327219 to your computer and use it in GitHub Desktop.
rails transactions
ActiveRecord::Base.transaction do
raise AcitveRecord::Rollback if something_bad_happens
end
def place_order
ActiveRecord::Base.transaction do
item = Item.some_scope
item.reserve!
order = current_user.new_order(item: item)
order.save!
invoice = order.new_invoice
invoice.save!
end
end
def place_order
item = Item.some_scope
item.reserve!
order = current_user.new_order(item: item)
order.save!
invoice = order.new_invoice
invoice.save!
end
object = ActiveRecord::Base.transaction do
item1 = Item.create_1
item2 = Item.create_2
[item1, item2]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment