Databases and Migrations

This blog post will build upon my previous post about creating a Rails app. I will now discuss adding a database to the app where data can be stored and accessed. Earlier I stated to enter the following in your terminal to create a new Rails app:

$rails new john-app

To create a new Rails app WITH A DATABASE, you would instead write the following:

$rails new john-app --database=postgresql

The “postgresql” refers to a specific database system called PostgreSQL which is the one we are using in the course. Once you have done this, navigate into the app folder:

$cd john-app

From here, run the following command:

$rails db:create

This creates the database within the app (in this case, PostgreSQL ). Now that the database is created, we need to create a table within the database where we can store information pertaining to our app. For example, let’s say our app will render a website which will sell certain products. We will need to create a table to store information about each of these products. To keep things very simple, let’s say we want to store two pieces of data for each product: the name of the product and its price. We would create this table by writing the following:

$rails generate model Product name 'price:decimal{5,2}'

Now let’s break down this command. The “rails generate model” is rails syntax that creates the table. “Product” is what I have chosen to call the table. What you put here is up to you, but it should make sense and represent the data that will be stored there. Also, note the format…it must be camel case and singular. After that come the attributes you want to store for each item (name and price in this case). You must specify a type for each attribute (you do this with a colon, no space) and you separate each attribute/type with a space. The default setting for an attribute is “string”, so you do not need to put the “:string” after an attribute you want to be a string. We also could have written the above as:

$rails generate model Product name:string 'price:decimal{5,2}'

The syntax for the price attribute is a bit more complicated. Since we are assigning it the type “decimal”, we have to wrap the entire thing in single quotes and specify a precision and scale within curly braces. You can take a look at this page for information about types and syntax:

Creating this table will create a migration file in the db/migrate folder in the app. It is advised to check this file for any errors/typos at this time. Before doing the next step, changes can be made to this file. Once you are sure the everything is in order, run the following command in the terminal:

$rails db:migrate

Running this command will migrate (add) the table to the existing database. Do not alter the migration file after this step. At this point, if a change needs to be made to the table, another migration must be created to make the change. For example, let’s say we want to add an image url as an attribute to our products. To do this, we would run the following command:

$rails generate migration AddImageToProducts

Breaking this command down, “rails generate migration” is Rails syntax for creating a migration and AddImageToProducts is a name of your choosing for the migration. The name should describe the change being made (what you are changing and what table in the database it is affecting) and again should use the camel case convention. Once the migration is generated, open the corresponding file in the db/migrate folder. There you will see something like the following:

class AddImageToProducts < ActiveRecord::Migration[5.0]
def change
end
end

Here is where you can add code to make the change to the database table. There are a variety of changes you can make. Continuing with our example of wanting to add an image url, you would add the following code to the “def change” method:

class AddImageToProducts < ActiveRecord::Migration[5.0]
def change
add_column :products, :image, :string
end
end

In the above code, “:products” refers to the table we want to modify, “:image” is the name of the attribute we want to add and “:string” defines the attribute type. Here we have made it a string because it will be a url. The following site is a useful resource for making changes in this manner:

After you have written the code to make the change, double check to make sure it is accurate before proceeding to the next step. Once you have done that, once again run the following in your terminal:

$rails db:migrate

This will add the migration to the database and institute your desired change. The products will now have the added attribute of an image url associated with them.

So, there you have it. You can now create a database and be another step further in your creation of an awesome Rails app!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.