0
0
Ruby on Railsframework~20 mins

Database folder and migrations in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you run rails db:migrate?

In a Rails project, what is the main effect of running the command rails db:migrate?

AIt backs up the current database and creates a new empty database.
BIt deletes all data from the database and resets the schema to the initial state.
CIt generates new migration files based on changes in the models automatically.
DIt applies all pending migration files in the <code>db/migrate</code> folder to update the database schema.
Attempts:
2 left
💡 Hint

Think about what migrations are for and what the db/migrate folder contains.

📝 Syntax
intermediate
2:00remaining
Identify the correct migration syntax to add a new column

Which of the following migration snippets correctly adds a string column named username to the users table?

Aadd_column :username, :users, :string
Badd_column :users, :username, :string
Cadd_column :users, :username, string
Dadd_column users, username, :string
Attempts:
2 left
💡 Hint

Remember the order of arguments for add_column is table name, column name, then type.

🔧 Debug
advanced
2:00remaining
Why does this migration fail to run?

Consider this migration code:

class AddAgeToUsers < ActiveRecord::Migration[6.1]
  def change
    add_column :users, :age, integer
  end
end

Why will this migration cause an error when running rails db:migrate?

Ruby on Rails
class AddAgeToUsers < ActiveRecord::Migration[6.1]
  def change
    add_column :users, :age, integer
  end
end
AThe migration version [6.1] is not supported.
BThe migration class name is incorrect; it should be plural.
CThe type 'integer' is not a symbol; it should be :integer.
DThe method 'change' is missing a 'super' call.
Attempts:
2 left
💡 Hint

Check the data type argument in add_column.

state_output
advanced
2:00remaining
What is the schema version after running migrations?

After running rails db:migrate, where does Rails store the current schema version number?

AIn the <code>schema_migrations</code> table inside the database.
BIn the <code>db/schema.rb</code> file as a Ruby constant.
CIn the <code>config/database.yml</code> file under version key.
DIn the <code>db/migrate</code> folder name.
Attempts:
2 left
💡 Hint

Think about how Rails tracks which migrations have been applied.

🧠 Conceptual
expert
3:00remaining
Why should migration files be immutable after deployment?

In a team project, why is it important not to edit existing migration files after they have been run in production?

ABecause changing migration files can cause inconsistencies between developers' databases and production, leading to errors.
BBecause Rails automatically locks migration files after deployment to prevent changes.
CBecause migration files are compiled into the application binary and cannot be changed.
DBecause editing migration files will delete all data in the database automatically.
Attempts:
2 left
💡 Hint

Consider what happens if different environments have different migration histories.