Update Table
Use the method to update existing tables. Like create_table, the change_table method accepts the name of the table and a block to add columns or indexes to the table:Rename Table
Use the to rename a table.Drop Table
To drop an existing table, you can use the drop_table method. It ignores the provided block and options when running the migration, but uses them when reverting the migration to generate the corresponding table. Create Columns
You can specify new columns while creating a new table. But if you want to add new columns to an existing table, use the add_column method. Modify Columns
The method allows you to modify the type and attributes of existing columns.Rename Column
To rename a column, use the method.Remove Column
To drop a column, you may use the remove_column method. Referencing Tables
To add foreign key, use the method. For example, if the posts table references a user, then posts will have a column in it. |
Data Types
Column Types
Create Index
ActiveRecord supports several types of indexes. Use the add_index or t.index method to add new index. Additionally, you can mark the index as unique or give it a different name. Rename Index
To rename an index, use the method. This method accepts the table name as its first argument and the current index name and the desired name as its second and third arguments, respectively.Alternatively, use the method while changing a table. This is useful when you want to change multiple index names in a single migration.Drop Index
To drop an index, you may use the method, passing the name of the table and either the column name or the name of the index. Alternatively, use the method while changing the table. |
Cheatography
https://cheatography.com
Active Record Migrations Cheat Sheet (DRAFT) by software_writer
Database migrations in Active Record
This is a draft cheat sheet. It is a work in progress and is not finished yet.