0
0
Ruby on Railsframework~5 mins

Adding and removing columns in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What command do you use to add a new column to an existing table in Rails?
You create a migration with rails generate migration AddColumnNameToTableName column_name:data_type and then run rails db:migrate.
Click to reveal answer
beginner
How do you remove a column from a table in Rails migrations?
Use remove_column :table_name, :column_name inside a migration and then run rails db:migrate.
Click to reveal answer
beginner
What is the purpose of the change method in a Rails migration?
The change method defines what changes to make to the database schema. Rails can automatically reverse these changes when rolling back.
Click to reveal answer
intermediate
Why should you avoid adding columns directly in the database without migrations in Rails?
Because migrations keep your schema consistent and track changes, making it easier to share and rollback changes safely.
Click to reveal answer
beginner
What data types can you specify when adding a column in Rails migrations?
Common types include :string, :text, :integer, :boolean, :datetime, and :float.
Click to reveal answer
Which Rails command generates a migration to add a column?
Arails generate migration AddAgeToUsers age:integer
Brails add column age to users
Crails db:add_column age users
Drails create column age users
How do you remove a column named 'email' from the 'customers' table in a migration?
Aremove :customers, :email
Bremove_column :customers, :email
Cdelete_column :customers, :email
Ddrop_column :customers, :email
What happens if you run rails db:rollback after adding a column with a migration?
AThe migration file is deleted
BNothing happens
CThe entire table is deleted
DThe added column is removed from the table
Which method inside a migration allows Rails to automatically reverse changes?
Adown
Bup
Cchange
Dmigrate
Why is it important to use migrations for schema changes in Rails?
ATo keep schema changes tracked and reversible
BTo speed up the app
CTo avoid writing Ruby code
DTo prevent database backups
Explain the steps to add a new column to an existing table in Rails using migrations.
Think about how Rails tracks database changes safely.
You got /3 concepts.
    Describe how Rails migrations help manage database schema changes like adding or removing columns.
    Consider why direct database edits can be risky.
    You got /4 concepts.