0
0
Ruby on Railsframework~15 mins

Changing column types in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Changing column types in Rails migrations
📖 Scenario: You are working on a Rails app that manages a library. You need to update the database to change the data type of a column to better fit the data it will hold.
🎯 Goal: Create a Rails migration to change the data type of the published_year column in the books table from string to integer.
📋 What You'll Learn
Create a migration file named ChangePublishedYearTypeInBooks
Use the change_column method to change published_year from string to integer
Ensure the migration is reversible
Use the correct Rails migration syntax
💡 Why This Matters
🌍 Real World
Changing column types is common when app requirements evolve and data needs to be stored differently.
💼 Career
Rails developers often write migrations to update database schemas safely and maintainably.
Progress0 / 4 steps
1
Create the migration class
Create a migration class named ChangePublishedYearTypeInBooks that inherits from ActiveRecord::Migration[7.0].
Ruby on Rails
Need a hint?

Start by defining a class with the exact name and inheritance shown.

2
Add the change method
Inside the ChangePublishedYearTypeInBooks class, add a change method definition.
Ruby on Rails
Need a hint?

The change method is where you put migration instructions.

3
Use change_column to modify the column type
Inside the change method, use change_column to change the published_year column in the books table from string to integer.
Ruby on Rails
Need a hint?

Use change_column :books, :published_year, :integer inside the change method.

4
Complete the migration class
Ensure the migration class is complete with the change method containing the change_column call and properly closed with end.
Ruby on Rails
Need a hint?

Check that all parts of the class and method are present and closed properly.