0
0
Ruby on Railsframework~3 mins

Creating migrations in Ruby on Rails - Why You Should Know This

Choose your learning style9 modes available
The Big Idea

What if you could change your database safely with just a few commands, no matter who's working on the app?

The Scenario

Imagine you have a growing app and need to change your database structure by adding a new column or table. You try to do it by manually editing the database directly on your computer or server.

The Problem

Manually changing the database is risky and confusing. You might forget what changes you made, cause errors, or break your app. It's hard to keep track of changes, especially when working with a team or deploying to different environments.

The Solution

Creating migrations in Rails lets you write simple, clear instructions to change your database. These instructions can be saved, shared, and run automatically, so your database stays organized and consistent everywhere.

Before vs After
Before
ALTER TABLE users ADD COLUMN age INTEGER;
After
rails generate migration AddAgeToUsers age:integer
rake db:migrate
What It Enables

It enables smooth, safe, and trackable database changes that work perfectly across all your development and production setups.

Real Life Example

When your app needs to store users' birthdays, you create a migration to add a birthday column. Everyone on your team runs the migration, and the app works the same everywhere without confusion.

Key Takeaways

Manual database edits are error-prone and hard to track.

Migrations provide a clear, repeatable way to change the database.

Migrations keep your app's data structure consistent and safe.