TAGS:

ruby  rails  migration  database 

Related:


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 CreateStudent

This 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: false

Also, You can edit this file and add or remove columns before making rake db:migrate

Then, Apply changes to database using:

rake db:migrate

Now, You must create student.rb model manually in app/models to deal with students table as:

class Student<ActiveRecord::Base
end

Note that you can undo this migration using:

rake db:rollback

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:

rails g CreateStudent
#to be
rails d CreateStudent

The last line will delete the migration file generted by the first line.

Now take a look to this tutorial video: