Junior Track Roadmap

ActiveRecord migrations: multi-table

This course covers ActiveRecord migrations that create or change several related tables in one file. Rails wraps each migration in one transaction by default, so a file lands completely or not at all. The lessons cover ordering create_table calls so a foreign key never points at a missing table. They explain when create_join_table fits and how to add a NOT NULL column to a table with rows. They also cover reversible, revert and ActiveRecord::IrreversibleMigration, and end with one combined example file.

What you'll learn

  • ✓Why related schema changes belong in one migration file and one transaction
  • ✓Ordering create_table calls so foreign keys reference tables that already exist
  • ✓When create_join_table fits and why its indexes must be added by hand
  • ✓Adding a NOT NULL column with a constant default to a table with rows
  • ✓Writing data steps with reversible, revert and explicit up/down methods
  • ✓Handling ActiveRecord::IrreversibleMigration when a rollback has no inverse
Free preview · Lesson 1 4 min read

One file, one transaction, one related change

"ActiveRecord: migrations" taught what a migration is. ActiveRecord migrations: indexes taught how to make a single add_index safe on a table that already holds real rows. Both stayed inside one operation on one existing table. Neither asked what happens when a feature needs several new tables at…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...