age from integer to string in a Rails migration?class ChangeAgeTypeInUsers < ActiveRecord::Migration[7.0] def change # change column type here end end
The change_column method takes the table name first, then the column name, and finally the new type.
Options A, B, and C use incorrect method names or argument orders.
price column when you change its type from integer to string using change_column in Rails migration?When changing a column type from integer to string, the database converts existing integer values to their string representations.
Data is preserved but converted.
class ChangeActiveToBooleanInUsers < ActiveRecord::Migration[7.0]
def change
change_column :users, :active, :boolean, default: false
end
endclass ChangeActiveToBooleanInUsers < ActiveRecord::Migration[7.0] def change change_column :users, :active, :boolean, default: false end end
No error. The change_column method accepts default as an option.
To change only the default without changing type, use change_column_default separately.
email column?class ChangeEmailTypeInUsers < ActiveRecord::Migration[7.0]
def change
change_column :users, :email, :text, null: false
end
endclass ChangeEmailTypeInUsers < ActiveRecord::Migration[7.0] def change change_column :users, :email, :text, null: false end end
change_column support changing null constraints directly?The change_column method supports the null option, setting the column to NOT allow null values.
For nullability-only changes, prefer change_column_null for better reversibility across databases.
string to integer but existing data may not convert cleanly. What is the best approach to handle this safely in Rails migrations?Changing column types directly can cause data loss or errors if data doesn't convert cleanly.
Best practice is to add a new column, migrate and clean data manually, then remove the old column.
This approach is safer and reversible.