Transaction

  • Use transaction blocks whenever you have a number of statements that must be executed together or not at all.
  • Transactions are per-database connection, not per-mode.

Transaction on ActiveRecord::Base#

ActiveRecord::Base.transaction do
# code...
end

Transaction on Model#

User.transaction do
# code...
end

Transaction on instance of Model#

user = User.new
user.transaction do
# code...
end

save and destroy are automatically wrapped in a transaction#

Resources:#