1 == Creating A Migration ==
3 === Creating a model ===
5 The model and scaffold generators will create migrations appropriate for adding a new model. This migration will already contain instructions for creating the relevant table. If you tell Rails what columns you want then statements for adding those will also be created. For example, running
7 `ruby script/generate model Product name:string description:text` will create a migration that looks like this
10 -----------------------
11 class CreateProducts < ActiveRecord::Migration
13 create_table :products do |t|
25 -----------------------
27 You can append as many column name/type pairs as you want. By default `t.timestamps` (which creates the `updated_at` and `created_at` columns that
28 are automatically populated by Active Record) will be added for you.
30 === Creating a standalone migration ===
31 If you are creating migrations for other purposes (for example to add a column to an existing table) then you can use the migration generator:
33 `ruby script/generate migration AddPartNumberToProducts`
35 This will create an empty but appropriately named migration:
38 -----------------------
39 class AddPartNumberToProducts < ActiveRecord::Migration
46 -----------------------
48 If the migration name is of the form AddXXXToYYY or RemoveXXXFromY and is followed by a list of column names and types then a migration containing
49 the appropriate add and remove column statements will be created.
51 `ruby script/generate migration AddPartNumberToProducts part_number:string`
56 -----------------------
57 class AddPartNumberToProducts < ActiveRecord::Migration
59 add_column :products, :part_number, :string
63 remove_column :products, :part_number
66 -----------------------
70 `ruby script/generate migration RemovePartNumberFromProducts part_number:string`
75 -----------------------
76 class RemovePartNumberFromProducts < ActiveRecord::Migration
78 remove_column :products, :part_number
82 add_column :products, :part_number, :string
85 -----------------------
87 You are not limited to one magically generated column, for example
89 `ruby script/generate migration AddDetailsToProducts part_number:string price:decimal`
94 -----------------------
95 class AddDetailsToProducts < ActiveRecord::Migration
97 add_column :products, :part_number, :string
98 add_column :products, :price, :decimal
102 remove_column :products, :price
103 remove_column :products, :part_number
106 -----------------------
108 As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit.