Junior Track Roadmap

ActiveRecord: transactions

This course covers ActiveRecord::Base.transaction in application code, for cases where several related writes must succeed or fail together. It tests which ways of leaving a block trigger a rollback: a raised exception rolls back, while a bare return or a falsy value commits. It shows ActiveRecord::Rollback for silent cancellation, nested blocks that join the outer transaction, and requires_new: true with savepoints. It then reads this app's CompleteLessonService and AccountUpdateService line by line and explains why save! and update! matter inside a transaction.

What you'll learn

  • ✓How ActiveRecord::Base.transaction makes a group of writes all or nothing
  • ✓Why only a raised exception rolls back, while return and falsy values commit
  • ✓When to use ActiveRecord::Rollback for a silent, expected cancellation
  • ✓How nested transactions join the outer one and what requires_new: true changes
  • ✓Why save! and update! decide whether earlier writes in a block are undone
Free preview · Lesson 1 3 min read

ActiveRecord::Base.transaction: all your writes, or none of them

ActiveRecord migrations: multi-table showed that Rails wraps an ordinary migration's whole change method in one database transaction, so several create_table calls either all land or none do. Nobody wrote that transaction by hand; Rails supplied it. This course covers the same guarantee written…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...