Ruby on Rails Changing Tables


If you have created a table with some wrong schema, then the easiest way to change the columns and their properties is change_table. Review the following example:

change_table :orders do |t|
  t.remove :ordered_at # removes column ordered_at
  t.string :skew_number # adds a new column 
  t.index  :skew_number #creates an index
  t.rename :location, :state #renames location column to state

The above migration changes a table orders. Here is a line-by-line description of the changes:

  1. t.remove :ordered_at removes the column ordered_at from the table orders.
  2. t.string :skew_number adds a new string-type column named skew_number in the orders table.
  3. t.index :skew_number adds an index on the skew_number column in the orders table.
  4. t.rename :location, :state renames the location column in the orders table to state.