6 ### Automatically make migrations
8 $ rails generate migration RemovePartNumberFromProducts part_number:string
9 $ rails generate migration remove_part_number_from_products part_number # rails assumes string if not type given - and you can use snake_case
11 $ rails generate migration AddNameToWidgets name:string
12 $ rails g migration add_name_to_widgets name:string # you can use the short cut 'g' instead of generate - they both do the same thing
20 create_table :users do |t|
27 t.integer :games_count
30 t.decimal :price, :precision => 2, :scale => 10
31 t.datetime :expiration
46 add_column :users, :first_name, :string
47 remove_column :users, :first_name, :string
49 change_column :users, :first_name, :text
51 change_column_default :users, :admin, nil
52 change_column_null :users, :email, false # adds NOT NULL constraint
68 class AddFlagToProduct < ActiveRecord::Migration
69 class Product < ActiveRecord::Base
73 add_column :products, :flag, :boolean
74 Product.reset_column_information
76 dir.up { Product.update_all flag: false }
83 t.references :category # kinda same as t.integer :category_id
85 # Can have different types
86 t.references :category, polymorphic: true
88 ### Auto-Add/remove columns
90 $ rails generate migration RemovePartNumberFromProducts part_number:string
95 add_index :suppliers, :name
98 add_index :accounts, [:branch_id, :party_id], :unique => true
100 # Named (:name => ...)
101 add_index :accounts, [:branch_id, :party_id], :unique => true, :name => "by_branch_party"
104 add_index :accounts, :name, :name => ‘by_name’, :length => 10
105 add_index :accounts, [:name, :surname], :name => ‘by_name_surname’,
111 # Sort order (no MySQL support)
112 add_index :accounts, [:branch_id, :party_id, :surname],
113 :order => {:branch_id => :desc, :part_id => :asc}
116 Use `ActiveRecord::Migration`.
118 ActiveRecord::Migration.add_index :posts, :slug
122 * http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index