Rails generate migration main commands create, delete, update.

Stela Capsa
3 min readJun 29, 2021
  1. Active Record Migration are commands that allowed to create tables and update the schema.rb file. Schema is the file that holds all the data base.
$ rails g migration [tableNames]

This command creates table. In db folder is generated a new table and it looks like this…

Here can be made any changes needed, add more columns or fix syntax errors etc. Afterwords is necessary to run rake db:migrate to update the schema and push the data to databases.

$ rake db:migrate

2. If is needed to add a column to an existing table, follow the next steps

$ rails g migration addColumnToExistingTable

This command will create a table in db folder. Below is represented the format and order on how we add a column to an existing table.

:onePage — is the table name

:email — is the column name

:string — is the format

Here it can be added multiple columns for example

After any change, save the file and run again …

$ rake db:migrate

3. In case is needed to delete a table , run the next command

$ rails g migration drop_tablename

This command deletes a table from DB and updates the schema.

4. In case we want to change the name of created table, here is the solution

$ rails g migration change[OldTableName]To[NewTableName]

Created file…

As mentioned before after saving the file is necessary to run…

$ rake db:migrate

This are main commands to create update and delete a table in Active Record Migration.

Scaffold is an other way to build tables along with controllers, models and view files.

Happy Coding!

--

--