How to remove a column in rails?

less than 1 minute read

Syntax

remove_column(table_name, column_name, type = nil, **options)

Example

remove_column(:users, :phone, type: :string)

While the type is not mandatory, it is advised to add it so that the type is used while reverting the migration.

Rails Command:

$ rails generate migration remove_phone_from_users phone:string
      invoke  active_record
      create    db/migrate/20230906141018_remove_phone_from_users.rb

The above command will generate an appropriate migration

class RemovePhoneFromUsers < ActiveRecord::Migration[7.1]
  def change
    remove_column :users, :phone, :string
  end
end