Adding Column Or More To An Existed Table In Rails
TAGS:
ruby rails migration database add
Related:
- Creating migration file using generate migration
- Creation migration file using generate model.
- Creating migration file using scaffold.
- Drop tables using migration.
- Editing migration file manually.</li>
- Seeding data into tables via migration.
- Joining two tables via migration.
- Migration specific VERSION or STEP.
- Removing fields from tables via migration.
- Setting rails environment via migration command.
- Setup, reset and drop database
</ul>
Adding Column Or More To An Existed Table.
The general form of `adding` field to table is: Example, Adding `price` column to `books` table: Then, `Columnname = Price` and `Tablename = Books` and `datatype = float`rails g migration AddColumnnameToTablename columnname:datatype
Then,rails g migration AddPriceToBooks price:float
If you need to add `many` columns to table you can use:rake db:migrate
Then,rails g migration AddDetailsToBooks pages:integer year:integer
Note that you can undo this migration back step by step using:rake db:migrate
This will delete the `price` column from the `books` table in the database itself. Note, Any `rake db:rollback` may delete stored data in the `targeted` columns and can't be restored again. Also, You can delete the migration file generated by converting `g`(for generate) to `d` (for destroy) in migration command as:rake db:rollback
The last line will delete the migration file generted by the first line. Take a look to this tutorial video:rails g AddPriceToBooks price:float #to be rails d AddPriceToBooks price:float