What\'s the syntax for dropping a database table column through a Rails migration?
Run the following command in your terminal:
rails generate migration remove_fieldname_from_tablename fieldname:fieldtype
Note: the table name should be in plural form as per rails convention.
Example:
In my case I want to remove the accepted column (a boolean value) from the quotes table:
rails g migration RemoveAcceptedFromQuotes accepted:boolean
See the documentation re: a convention when adding/removing fields to a table:
There is a special syntactic shortcut to generate migrations that add fields to a table.
rails generate migration add_fieldname_to_tablename fieldname:fieldtype
# db/migrate/20190122035000_remove_accepted_from_quotes.rb
class RemoveAcceptedFromQuotes < ActiveRecord::Migration[5.2]
# with rails 5.2 you don't need to add a separate "up" and "down" method.
def change
remove_column :quotes, :accepted, :boolean
end
end
rake db:migrate or rails db:migrate (they're both the same)
....And then you're off to the races!