Um, so, er...what is ActiveRecord again?
Tags: ruby, beginners
Published: 2020-10-21 07:23:58

<img width="100%" style="width:100%" src="https://media.giphy.com/media/iAYupOdWXQy5a4nVGk/giphy.gif">

I got introduced to ActiveRecord just a few days ago, and it made my head spin. It seemed like it did a zillion different things but I couldn't pin it down enough to explain it, even to myself. After a lot more experience coding with it, I'm gonna try.

#### What is it?

ActiveRecord is a Ruby gem, which is a library of code we get to use by running `gem install activerecord` or by writing including `gem "activerecord"` in your Gemfile.

#### What does it do?

This is a hard one, because ActiveRecord does a *lot*. The two main things it does have to do with object instantiation. When you initialize an object, you want it to persist beyond just the runtime of the program. This requires storing information about the object in a database. Wouldn't it be cool if every time you create an object, it was automatically added to a database and gave it an ID number? Well, ActiveRecord lets you do that by using a method called `create` like 

```ruby
Ghost.create("Blinky")
```
instead of 
```ruby
Ghost.new("Blinky")
``` 

The other great thing it does is it automatically gives you a lot of object and class methods you'd otherwise have to create for yourself. What methods, you ask? 
* `find` by id number, like `Ghost.find(3)`
* `find_by` method, like 
```ruby
Ghost.find_by(name = 'Blinky')
```
* getters and setters for object attributes *without having to write* `attr_accessor`s, like 
```ruby
ghost.name = 'Blinky'
```
or
```ruby
ghost.name
=> 'Blinky'
```

How? When you define a class, you just make it inherit from the ActiveRecord class (specifically, one of ActiveRecord's sub-classes), like this:
```ruby
class Ghost < ActiveRecord::Base
end
```
All those attributes and methods listed above (and a whole lot more) are defined in the ActiveRecord::Base class somewhere.

#### ANALOGY

Let's start with ActiveRecord's enabling you to conveniently store every object you create in a database for later use. Analogies are hard, so I reserve the right to come up with a better one that this, but: Imagine you're trying on a new pair of glass frames at Warby Parker, one after another. ActiveRecord is your friend with a Polaroid camera snapping pics of you modeling every frame you like and writing its name on the photo with a sharpie so that you can reference them later and choose which one you want.

<img width="100%" style="width:100%" src="https://media.giphy.com/media/l0HlQXcnyJEYBVsxa/giphy.gif">

Next is ActiveRecord's ability to automatically give you a bunch of class methods without you having to write them yourself. In this way, ActiveRecord is like...um...an Instant Pot! Before, you had a slow cooker, a rice cooker, a sous-vide immersion thing, and a pressure cooker. With your fancy new Instant Pot Duo Evo, you can give all those appliances away because the Instant Pot has all that functionality built-in. Ok, yeah, this is a weak analogy but it's 11:51pm and I've been coding all day.

<img width="100%" style="width:100%" src="https://media.giphy.com/media/ppyvw6iUQjdja/giphy.gif">

#### ABSENCE

Ok, this is easy, because ActiveRecord replaces a bunch of work you would have had to do otherwise. Without ActiveRecord, you would have to:
* have to write your own custom Object Relational Mappings (ORMs)
* write your own code to add newly created objects to a database
* find and update the corresponding database entry for an object every time changes are made to that object
* write your own getter and setter code for every object attribute you want to write to and read from

<img width="100%" style="width:100%" src="https://media.giphy.com/media/3DnDRfZe2ubQc/giphy.gif">

#### EXAMPLE

```ruby
gem 'activerecord'

class Ghost < ActiveRecord::Base
end

pac_man_ghosts = ["Blinky", "Pinky","Inky", "Clyde"]
pac_man_ghosts.each do |ghost|
    Ghost.create(name: ghost)
end
```
![Pacman](https://user-images.githubusercontent.com/15787073/96745143-aa5b9a80-137a-11eb-81ca-09c224bc1e31.png)

Provided you set everything up correctly (which includes connecting a database and writing the necessary migrations, which is a whole 'nother ball of wax), you now have 4 pac-man ghost objects that 
1. ...are stored in a database
2. ...have their own getters and setters
3. ...have their own `find` and `find_by` methods
(...and a bunch of other stuff I'm not going to spend time on right now)

If you have any better analogies for ActiveRecord, I wanna hear 'em! If they're good enough to replace the ones I came up with in this blog post and you live in the Bay Area, I will bake you a bundt cake (my pandemic hobby) of your chosen flavor. If you don't live in the Bay Area, I will figure out how to mail you one.

<img width="100%" style="width:100%" src="https://media.giphy.com/media/LTFMJeQSHHcwsLWvjd/giphy.gif">