Migrations Methods
Learn about the various methods in Rails migrations and how to use them.
We'll cover the following
Writing your own migrations
The generate
command only provides you with a template to work off of. It will create a single table with the specified attributes, and a migration will be generated for the command. To really make use of migrations, you need to be familiar with the various methods it contains.
Note: A migration file is created when the command is run for the first time. Trying to use the
generate model
command with the same model name will give an error. You will have to run migration commands to make changes to the model.
Up and down methods
The up
and down
methods perform opposing functions. Whatever transformation is made by your up
method should be exactly reverted when you run the down
method. Essentially, running the up
and down
methods in succession should leave the database schema unchanged.
If your up
method creates a table, your down
method should drop it. It is recommended that the reversal of the transformations using the down
method be done in reverse order.
Get hands-on with 1400+ tech skills courses.