Getting started with ActiveRecord
Tags: ruby, beginners, activerecord, codenewbie
Published: 2022-09-16 16:44:22

I have been spending the last 3 weeks learning about backend development using Ruby, this has been a strange journey because I honestly thought backend development would be the most boring thing ever, databases all day? I was wrong.

We started off learning the basics about Ruby, which really is a delight to code in. However, I didn't realize how cool backend development could be until we started learning about ORM's and more specifically, ActiveRecord. I didn't really understand what an ORM was or why it would make my life easier, so, an ORM- is an Object_relational mapper. Really, It just provides different ways to convert data from one type system to another by using the language you are currently working with; then you can create queries to the database. This dramatically cuts down on the code that is required to be written.
Basically, this just means we were given a Ruby gem that can make our lives easier and the code that we write more DRY and concise. Not to mention all the great built in methods we get with ActiveRecord, which  I'll touch on later. But, for now, here are some steps to get you started using ActiveRecord.

I start by drawing out what I want my tables to look like, what are the attributes? How are the tables related? What kind of foreign keys do I need to associate my tables? ActiveRecord associations could be an entire blog post of its own, so I strongly encourage that you dig into that as well. Once I have an idea of what I want those tables to look like I can create a migration, by using ActiveRecord; I am giving it everything I want to be included in that table. A migration is like a blueprint for what you want your data to look like. The command you would need to use to start this process is 
`` rake db:create_migration NAME=create_tables ``

---
**As a very important side note, naming conventions are extremely important when using ActiveRecord. Above, "tables" would be the name of your table, its important that things are CAPITALIZED and made plural in this syntax **

---
After running this command, you will notice a couple new files that are created for you, these are your migrations files. It is super important that you don't change or rename these files, all the numbers at the beginning are a timestamp, and they are a kind of version control so you can keep your blue prints up to date and organized.


Next, I write what I want my blue prints to look like in those new migration files, remember, each migration file is a blue print to make your database table. 

```
class CreateFriends < ActiveRecord::Migration[6.1]
   def change
     create_table :friends do |t|
      t.string :name
      t.string :e_mail
      t.integer :age
       end
     end
  end

```

|name    |      email       |  age  |
|--------|:----------------:|------:|
|Victor  |victor@fake.com   |  36   |
|Jayce   |jayce@fake.com    |  28   |
|Mareena |mareena@fake.com  |  22   |



Once you have your migrations set up run the command ``rake db:migrate `` to make those blue prints and store them in your schema file (this is another one you dont want to directly edit or modify, this is part of what ActiveRecord helps with, keeping your data tidy so you can get what you, or someone else, needs from it, your schema is like the keeper of your up to date blue prints.)

Next step is to set up some dummy data to populate your shiny new tables, there are additional Ruby gems like Faker, as well as loops to automate this task but as a beginner I preferred to really control and check my data, and associations. 
After getting something in your seeds file to work with you want to run the command `` rake db:seed``

The last step to get you start with ActiveRecord is creating your models for each table, this is where you will make the associations so your data(tables) can interact, while remaining easier to organize, maintain and flexible to work with. 

---

These relationships also have specific syntax with the capitalization of the class name, what is plural and what is singular, as well as where commas and colons are placed. You can see an example in the blog photo, notice that

```
 Song belongs_to :artist
//(a single artist) 
Genre has_many :songs
//songs-plural, and it 
has_many :artists, through: :songs
//artists and songs are plural here.
```
 

(this is set up as a many to many relationship, in case you were wondering.)



---
If you have gotten this far, you can now create and manage a database structure, and make a whole lot of really useful and cool queries to your database using ActiveRecord as well as Ruby methods (I found myself at [APIdock](https://apidock.com/) often looking up syntax and all the possibilities of how to use these built in methods)

So, go use a cool debugging tool to play with the data a bit, check out how to access different parts of the data together, and even try out some different methods to make some queries. 

On a personal note- I was totally sleeping on how awesome the Ruby REPL debugging **binding.pry** is, so you might want to start with that, and maybe I should go write a blog about that little bit of magic.