Building your Database in Rails: A How-To

Building a Database

If you don’t already know what a database is, refer to my previous post from Week 5. Today, we are going to walkthrough creating a database and associated tables inside of the Rails framework, so buckle up!

First things first, we need to create a new Rails project. To do this, go to your terminal and navigate to the directory you would like the project to exist. Once you have done that, type in the following: rails new blog-project. Now, if you go to your text editor (I use Atom) and open up the directory for blog-project you will see a whole slew of files and folders automatically generated for you!

We are now on the right track! Now, go find a folder in there called db this should have a file inside of it called, seed.rb. There is a couple of problems here:

  1. There is no database!
  2. There are no migrations!
  3. The seeds file is empty!

Let’s fix this real quick. Navigate to the config folder and look for a file called database.yml and then look for the development section, there you will find a line that reads as follows:  database: blog-project_development.

rails 2

This is the name of your database. Now go back to your terminal and type in the following rake db:create.

This command will generate a new database, the name of which is located in your database.yml file we just looked at. Unfortunately, our database doesn’t have anything in it right now – it’s empty! Let’s fill it up with some tables.

In ActiveRecord we have things called tables which represent a collection of database objects that are similar. An example would be a table called cars which could have many different types of cars inside of that table.

Additionally, our tables have columns – these represent specific attributes of our database objects. We could have columns for make, model, color and so on.

For the purposes of this exercise we are going to want our users to be able to create a new blog post and post it online. So let’s do it! Go back to your terminal and type in the following command: rails generate migration create_articles.

rails 1

Now, go back to your db folder and you will notice something really cool, Rails has automatically created a new folder for you called migrate which has a migration file inside of it – open that up!

rails 4

This is where we will be creating our table in ActiveRecord. Pay attention to the line, create_table :articles do |t|. This is saying, create_table – this is a specific ActiveRecord method for creating a new table, :articles – the name of the table following by do |t|– this iterating through and generating all of the columns we want to be created for our table, called :articles. Let’s do that now.

What kind of things do we want our user to have access to? We should want them to be able to type in the Title of the blog post as well as the Body, which is the meat of the article. So, modify your migration file to look like this:

rails 5.png

This will now create a table with 2 columns: title and body. The t. part is just the syntax for ActiveRecord to iterate through using this method – don’t get too hung up on it, just know it works and we need it. Secondly, the null: false part is saying, this field is necessary, it cannot be blank – we will do further validations in our model file.

Now, in order to actually run this and then save these changes to the database we need to hop back over to our terminal and run the following command: rake db:migrate. This should result in the following:

rails 6.png

Great! We now have a table, but we need a way to interface with it using Ruby. We do this by creating a ruby class that that acts as a mediator between our controller and our database object. So, go into your models folder and create a file called article.rb. Inside of that files type in the following:

rails 9.png

You may be wondering what this nonsense is with inheritance. Inside of your models folder find your file – application_record.rb, you will find that it inherits from ActiveRecord::Base, and in turn our model classes inherit from ApplicationRecord, this is the Rails convention way of inheritance. It is something you will always do.

The validates: ... sections are simply checking to see if the user inputted information into these fields. This is good if we want to set up flash error messages to prompt the user to correct their input, but that is for another day.

 

Now, we have both a database as well as a table! Let’s confirm this by loading up our Rails application into PRY by going into our terminal and running the following command: rails c.

rails 8.png

This gives us behind-the-scenes access to our rails application – from here we can check on our database and its objects and tables, so let’s do just that. Type the following into the terminal: Article.create!(title: 'How to Create a Database', body: 'Insert lengthy blog post about making databases'). This should look something like this:

rails10.png

Here you can see, we used the create! method to not only create but persist (save) the data to the database table. We can confirm this by running the following command while still inside of PRY: > Article.all

rails 11.png

Here, we have a newly created Article object with the following attributes: id, title, body and 2 timestamps for created_at and updated_at.

It may not seem like much but you just created a database in ActiveRecord! Congratulation, you are well on your way to creating a full-on blogging website! From here, you would need to set up your routes.rb file, create controllers to handle HTTP requests and set up a few view files for displaying our information to the end user.

All of that will be covered a later blog post so stay tuned!

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s