23
Basics of Creating Tables With Active Record Migrations
Over the past six months, I have been learning the ins and outs of front-end web development as part of my school program. Whenever I would write a front-end application that would send fetch requests to a back-end, it was done to an independent JSON server which I hadn't set up myself. Now that I have been learning Ruby, I was finally able to build an application which made fetch requests to my own database. This quick tutorial will walk you through the basics of creating a table in a database with Ruby using migrations, assuming you have installed the necessary dependencies from your Gemfile.
Ruby uses Active Record to fulfill the model component of the model-view-controller (MVC) paradigm. It's a an object-relational mapping (ORM) framework for Ruby. Active Record is designed to be used with very specific naming conventions, and commonly followed organization conventions, to facilitate speed and ease of use.
At the top level of the directory, there should be a db folder (database). You should make sure there is a migrate folder under the db folder. This migrate folder will be where Active Record saves your database migrations in the exact order in which you execute them, which is useful for keeping track of version history in your database.
Naming convention is extremely important when using Active Record. When creating a table, the table name needs to be plural. For this example, I will be using pets. In your CLI you run
bundle exec rake db:create_migration NAME="create_pets"
This will create a ruby file under your migrate folder which is automatically named beginning with a timestamp, and ending with your migration name. In this case "create_pets".
For example:
20211206094530_create_pets.rb
Within this file, there will exist a template generated for you which will look like this:
class CreatePets < ActiveRecord::Migration[6.1]
def change
end
end
Now you can edit this file to add columns into the table in your database with data types of your choosing. The best way to do so is to set up an iteration in your new class using the create_table
method:
class CreatePets < ActiveRecord::Migration[6.1]
def change
create_table :pets do |t|
t.string :name
t.string :breed
t.boolean :spayed_neutered
t.integer :owner_id
t.timestamps
end
end
end
This will create a table with seven columns titled in order as id, name, breed, spayed_neutered, owner_id, created_at, and updated_at.
The id column is generated automatically by Active Record and is SQL's way of keeping track of all data it has in the table. The created_at and updated_at columns are generated with the t.timestamps shortcut and is considered good practice to include in all of your tables. The owner_id column is there to show how you would relate this table to a separate hypothetical owners table by way of a foreign key.
In order to implement the changes you have made to the 20211206094530_create_pets.rb
file, you have to run the following command in your CLI:
bundle exec rake db:migrate
You should now see a schema.rb file generated in your db folder, populated with:
ActiveRecord::Schema.define(version: 2021_12_03_225736) do
create_table "pets", force: :cascade do |t|
t.string "name"
t.string "breed"
t.boolean "spayed_neutered"
t.integer "owner_id"
t.datetime "created_at"
t.datetime "updated_at
end
end
The schema.rb file shows the structure of your table in the database.
If you realize you have made a mistake after executing your db:migrate
command, you can run bundle exec rake db:rollback
to bring yourself back one version history, edit the file, and run bundle exec db:migrate
again.
There you have it, the basics of creating and editing a simple table in Active Record using Ruby. We used the create_table
method in this example, but naturally there are many more methods at your disposal. You can find them, and more information, here.
23