0
0
Ruby on Railsframework~20 mins

Creating migrations in Ruby on Rails - Try It Yourself

Choose your learning style9 modes available
Creating Migrations in Rails
📖 Scenario: You are building a simple blog application. You need to create a database table to store posts with their titles and content.
🎯 Goal: Create a Rails migration to add a posts table with title and content columns.
📋 What You'll Learn
Create a migration file named create_posts
Add a posts table with title as a string column
Add a content column as text
Include timestamps in the table
💡 Why This Matters
🌍 Real World
Migrations help developers safely change the database structure as the app grows, like adding new tables or columns.
💼 Career
Understanding migrations is essential for backend and full-stack developers working with Rails to manage database changes efficiently.
Progress0 / 4 steps
1
Generate the migration file
Run the Rails command to generate a migration named create_posts.
Ruby on Rails
Need a hint?

Use rails generate migration create_posts to create the migration file.

2
Define the posts table structure
In the generated migration file, add a create_table :posts block with columns title as string and content as text.
Ruby on Rails
Need a hint?

Use create_table :posts do |t| and add t.string :title and t.text :content inside the block.

3
Add timestamps to the posts table
Inside the create_table :posts block, add t.timestamps to include created_at and updated_at columns.
Ruby on Rails
Need a hint?

Add t.timestamps inside the create_table block to add time columns.

4
Run the migration to update the database
Run the Rails command to apply the migration and create the posts table in the database.
Ruby on Rails
Need a hint?

Use rails db:migrate to apply the migration.