Phase 3 Ruby - Active Record Migrations - Creating Migration
Tags: ruby, webdev, beginners, programming
Published: 2022-06-06 03:19:48

As I was learning Ruby in phase 3, I found myself really enjoying the process of setting up a backend data server using Active Record. Active Record is an object relational mapper that makes it easier for developers to write code. Here, I will be discussing specifically creating Active Record migrations using Rake Tasks. Migrations are a convenient way to alter your database schema over time in a consistent way. There are a few steps that you need to follow.

1. You need to create a migration for setting up our data table (we will use "students" for this example) 

```
#put this code in your terminal
#make sure you are in your project's directory that you are 
#currently working or building 
bundle exec rake db:create_migration NAME=create_students
```
running this code will generate a new file in db/migrations called timestamps_create_students.rb

├── app
│   └── models
│       └── student.rb
├── config
│   └── environment.rb
├── db
│   └── migrate
│       └── 20220605095220_create_students.rb # new file here
├── spec
├── Gemfile
├── Gemfile.lock
└── Rakefile

The timestamp at the beginning of the migration will be used as part of the version control for the migrations and ensure they are run in the correct order.

Also, this rake task will add some code for us.

```
# db/migrate/20220605095220_create_students.rb
class CreateStudents < ActiveRecord::Migration[6.1]
  def change
  end
end
```

2. to finish CreateStudents migrations which will generate students table with appropriate columns for the data, we need to use create_table method and pass the name of the table we want to create as a symbol.

After the table name :students we write a block of code that is passed a block parameter t, which is a special Active Record migration object that helps add different columns to the table.

```
# db/migrate/20220605095220_create_students.rb
def change
  create_table :students do |t|
  end
end
```

3. we add columns.

```
class CreateStudents < ActiveRecord::Migration[6.1]
  def change
    create_table :students do |t|
      t.string :first_name
      t.string :last_name
      t.integer :age
      t.string :school
      # the id column will be generated automatically for every table.
    end
  end
end
```

4. Finally, we run our migration in the command terminal.

```
bundle exec rake db:migrate

== 20220605095220 CreateStudents: migrating ====================================
-- create_table(:students)
 0.0008s
== 20220605095220 CreateStudents: migrated (0.0009s) ===========================
```

Running this command:

* Active Record will create a new database file, if one doesn't already exist, based on the configuration in the database.yml file.
* It will then use the code in the migrate folder to update the database.
* It will also create a db/schema.rb file, which is used as a "snapshot" of the current state of your database.

You will see your db/schema.rb file look like the below:

```
ActiveRecord::Schema.define(version: 2022_06_05_095220) do
  create_table "students", force: :cascade do |t|
    t.string "first_name"
    t.string "last_name"
    t.integer "age"
    t.string "school"
  end
end
```
Use this Rake task to see your migration status to check whether the migration has updated the database.

```
bundle exec rake db:migrate:status

database: db/development.sqlite3

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20220605095220  Create students
```

Now you have successfully created migration using Rake Tasks!