Editing Migration File Manually In Rails
TAGS:
ruby rails migration database
Related:
- Adding fields to table via migration.
- Creating migration file using generate migration
- Creation migration file using generate model.
- Creating migration file using scaffold.
- Drop tables using migration.
- 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
Editing Migration File Manaully In Rails.
The general form is rails g migration and then you can create students table and don’t state columns names and its datatypes as:
rails g migration CreateStudentThis will create migration file in db/migrate folder to create students table.
Note, The generated file doesn’t contain any fields so, you can add:
t.string :name
t.date :birthdate
t.text :address
t.timestamps null: falseAlso, You can edit this file and add or remove columns before making rake db:migrate
Then, Apply changes to database using:
rake db:migrateNow, You must create student.rb model manually in app/models to deal with students table as:
class Student<ActiveRecord::Base
endNote that you can undo this migration using:
rake db:rollbackNote, 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:
rails g CreateStudent
#to be
rails d CreateStudentThe last line will delete the migration file generted by the first line.
Now take a look to this tutorial video: