ActiveRecords in Ruby and SQL: A Flatiron School Post
db:migrate? More like db:migraine
I’m currently a software engineering student at Flatiron School located in Dumbo in Brooklyn, New York, a very up-and-coming area with a lot of tech startups, and we just finished our first Module, sort of like finishing the first semester in college. This post is specifically about a lab assignment that we were required to complete, creating a new table using ActiveRecords, a topic that I really struggled with.
Let’s say you have a program and you want that program to interact with a database so that you can manipulate data. For the RECORD, this post will be written in the programming languages Ruby and SQL. Ruby is a back-end language, whereas SQL (which stands for Structured Query Language) is used to interact with data.
Ultimately, we’re trying to create table that looks something like this.
The school was nice enough to give us a folder (or a “directory” in technical terms) with all the necessary “Gems” in Ruby needed to create a table.
So let’s hop into it! I’m going to be writing my code in the
lib folder. And in that folder, I’m going to make a new file called
NewStudent.rb. I’m naming it
NewStudent because conventionally, each file only contains one class and the file is usually named after the class. This convention is just to keep things neat and nicely organized.
.rb stands for Ruby, which tells the computer that the file is going to be written in the Ruby language and to interpret it as so.
Okay. Now we can have some fun and start coding!
Let’s start off by creating a NewStudent class. I’m using the text editor Microsoft Visual Studio which gives me all the colorful words.
Now let’s define the
We’re going to store all the SQL code in a variable that I’m going to name
sql_code_goes_here. What you see after the assignment (or simply the
= sign) is called “interpolation”.
Now let’s create the actual table!
IF NOT EXISTS just checks to make sure that the
students table isn’t already created yet.
CREATE TABLE is the real magic here, which creates the
id INTEGER PRIMARY KEY initializes an ID column for the students table, which is defined as an integer or a number. The
id for a class is usually “automatically” generated each time a new item, or a row in the table if you will, is added.
Now we can start creating other columns in our table to store data about students. For instance, we can create a column called
name that just stores the “text” of each student’s name.
We can also create a column called
grade to store the report card grades. Feel free to continue experimenting and adding columns to the table, such as the students’ favorite foods, favorite hobbies, favorite colors, teachers, friends, etc.
Now that we have our table set up, we have to connect to the database using
execute the SQL code by passing in
sql_code_goes_here as a parameter or argument.
I’m embedding my code here so you can play around with it if you’d like!
And voila! We’re done! Here’s a dog pic to celebrate.