Database Migrations
A migration is a set of changes we want to make to our database.
By defining migrations, we’re telling Rails how to change the database to add, change, or remove tables, columns or other attributes of our database. This helps keep track of changes we make in development (only on our computer) so they can be deployed to production (live, online!) safely.
In the editor, open the migration Rails created for us so we can see what the migration does. This is located in :
class CreateProducts < ActiveRecord::Migration[8.0] def change create_table :products do |t| t.string :name
t.timestamps end endend
This migration is telling Rails to create a new database table named products
.
The create_table
block then defines which columns and types should be defined
in this database table.
t.string :name
tells Rails to create a column in the products
table called
name
and set the type as string
.
t.timestamps
is a shortcut for defining two columns on your models:
created_at:datetime
and updated_at:datetime
. You’ll see these columns on
most Active Record models in Rails and they are automatically set by Active
Record when creating or updating records.
- Preparing Ruby runtime
- Create development database