Last active
August 29, 2015 14:16
-
-
Save coalwater/4fe828c7221c8c327219 to your computer and use it in GitHub Desktop.
rails transactions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecord::Base.transaction do | |
raise AcitveRecord::Rollback if something_bad_happens | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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