age of type integer to the users table in a Rails migration?class AddAgeToUsers < ActiveRecord::Migration[7.0] def change # Add column here end end
The add_column method requires the table name first, then the column name, and then the type as a symbol. Option A follows this pattern correctly.
Option A swaps table and column names, causing an error.
Option A misses the colon before integer, causing a syntax error.
Option A misses the colon before users, causing a syntax error.
email column in the customers table?class RemoveEmailFromCustomers < ActiveRecord::Migration[7.0] def change remove_column :customers, :email, :string end end
remove_column does in Rails migrations.The remove_column method deletes the specified column from the table. The type argument is optional but allowed for safety.
Option D is incorrect because it does not rename columns.
Option D is incorrect because the type argument is allowed but optional.
Option D is incorrect because the column is removed, not just nulled.
class RemoveStatusFromOrders < ActiveRecord::Migration[7.0] def change remove_column :orders, :status end end
The remove_column method can be called with just the table name and column name. The type argument is optional.
Option A is incorrect because the type is optional.
Option A is incorrect because the syntax is correct.
Option A is incorrect because remove_column does not require a block.
products table have?class AddDescriptionToProducts < ActiveRecord::Migration[7.0] def change add_column :products, :description, :text end end class RemovePriceFromProducts < ActiveRecord::Migration[7.0] def change remove_column :products, :price, :decimal end end
The first migration adds the description column.
The second migration removes the price column.
Both migrations run successfully and change the table accordingly.
Option C is incorrect because price is removed.
Option C is incorrect because description is added.
Option C is incorrect because Rails supports multiple migrations modifying the same table.
change method when using add_column and remove_column in a migration?Rails can reverse add_column automatically by removing the added column.
However, remove_column cannot be reversed automatically because Rails does not know the column's type or options unless explicitly provided.
Therefore, to make remove_column reversible, you must provide extra information or write separate up and down methods.