Ruby on Rails / Models and Active Record

Handling Migrations and Database Schema

This tutorial will guide you through handling migrations and managing your database schema in Rails. We will cover how to create migrations, modify your database schema, and manag…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explores how to work with models, migrations, and Active Record in Rails.

Introduction

This tutorial aims to guide you through the process of handling migrations and managing your database schema in Rails. By the end of this tutorial, you should be able to understand and use Rails migrations to create, modify, and manage different versions of your database schema effectively.

You will learn:
- What Rails Migrations are and why they're important.
- How to create, modify and rollback migrations.
- Managing your database schema using Rails Migrations.

Prerequisites:
- Basic knowledge of Ruby on Rails.
- A Rails application set up on your local machine.

Step-by-Step Guide

Rails migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent.

Creating a Migration:
Migrations are typically created with the rails generate command followed by migration and the name of your migration.

rails generate migration AddPartNumberToProducts part_number:string

This will create a migration file in the db/migrate directory.

Running Migrations:
To apply the changes defined in the migration file to your database, you run the migrations using the rails db:migrate command.

Rolling Back Migrations:
Migrations can also be rolled back using the rails db:rollback command, which will undo the last migration command.

Code Examples

Example 1: Creating a Migration

class AddPartNumberToProducts < ActiveRecord::Migration[6.0]
  def change
    add_column :products, :part_number, :string
  end
end

This migration adds a new column, part_number, to the products table.

Example 2: Modifying a Migration

class ChangePartNumberFormatInProducts < ActiveRecord::Migration[6.0]
  def up
    change_column :products, :part_number, :integer
  end

  def down
    change_column :products, :part_number, :string
  end
end

This migration changes the type of the part_number column from string to integer.

Summary

In this tutorial, we've covered how to create, modify, and manage your database schema using Rails migrations. We've also learned how to roll back changes and why migrations are essential for database schema management.

Practice Exercises

  1. Create a migration to add a reviews table with content (text), user_id (integer), and product_id (integer).

  2. Modify the reviews table to change the content column into a string type.

  3. Roll back the last migration you applied.

Additional Resources

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Unit Converter

Convert between different measurement units.

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help