0
0
Ruby on Railsframework~10 mins

Adding and removing columns in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Adding and removing columns
Generate Migration
Edit Migration File
Run Migration
Database Schema Updated
Use Updated Model
The flow shows creating a migration, editing it to add or remove columns, running it to update the database, and then using the updated model.
Execution Sample
Ruby on Rails
class AddAgeToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :age, :integer
  end
end
This migration adds an integer column 'age' to the 'users' table.
Execution Table
StepActionMigration File ContentDatabase Schema ChangeResult
1Generate migrationclass AddAgeToUsers < ActiveRecord::Migration[7.0] def change end endNo change yetMigration file created
2Edit migrationclass AddAgeToUsers < ActiveRecord::Migration[7.0] def change add_column :users, :age, :integer end endNo change yetMigration file updated with add_column
3Run migrationSame as step 2users table gains 'age' column of type integerSchema updated, column added
4Use modelN/ASchema includes 'age' columnCan now read/write 'age' attribute on User
5Generate remove migrationclass RemoveAgeFromUsers < ActiveRecord::Migration[7.0] def change remove_column :users, :age end endNo change yetMigration file created to remove column
6Run remove migrationSame as step 5users table loses 'age' columnSchema updated, column removed
7Use modelN/ASchema no longer has 'age' columnAccessing 'age' attribute will fail
8ExitN/ANo further changesProcess complete
💡 All migrations run, database schema updated accordingly
Variable Tracker
VariableStartAfter Step 3After Step 6Final
users table columnsid, name, emailid, name, email, ageid, name, emailid, name, email
Key Moments - 3 Insights
Why doesn't the database schema change immediately after generating the migration file?
Because generating the migration only creates a file; the schema changes only after running the migration (see execution_table step 3).
What happens if you try to access a removed column in the model?
Accessing a removed column will cause errors because the database no longer has that column (see execution_table step 7).
Why do we specify the column type when removing a column?
Specifying the type helps Rails know exactly which column to remove, ensuring safe migration (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the 'age' column actually added to the database?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Check the 'Database Schema Change' column in the execution_table rows
According to the variable tracker, what columns does the users table have after step 6?
Aid, name, email, age
Bid, name, email, age, created_at
Cid, name, email
Dage only
💡 Hint
Look at the 'After Step 6' column in variable_tracker
If you forgot to run the migration after editing the migration file, what would happen?
AThe schema would remain unchanged
BThe database schema would update automatically
CThe migration file would run on its own
DRails would throw an error immediately
💡 Hint
Refer to execution_table steps 2 and 3 to see when schema changes
Concept Snapshot
Adding and removing columns in Rails:
- Generate a migration file with 'rails generate migration'
- Edit the migration to add_column or remove_column
- Run 'rails db:migrate' to update the database
- Use the updated model with new or removed columns
- Always specify column type when removing columns
Full Transcript
In Rails, to add or remove columns from a database table, you first generate a migration file. This file is like a recipe for changes but does not affect the database until you run it. You then edit the migration file to specify adding or removing a column, including the column name and type. Running the migration applies these changes to the database schema. After that, your Rails model reflects the updated schema, allowing you to use the new or removed columns in your code. Remember, generating a migration file alone does not change the database; running the migration is necessary. Also, when removing columns, specifying the column type helps Rails safely identify the column to remove.