DEV Community

Analogy | Absence | Example
Analogy | Absence | Example

Posted on • Edited on

Um, so, er...what is ActiveRecord again?

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

Ghost.create("Blinky")
Enter fullscreen mode Exit fullscreen mode

instead of

Ghost.new("Blinky")
Enter fullscreen mode Exit fullscreen mode

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
Ghost.find_by(name = 'Blinky')
Enter fullscreen mode Exit fullscreen mode
  • getters and setters for object attributes without having to write attr_accessors, like
ghost.name = 'Blinky'
Enter fullscreen mode Exit fullscreen mode

or

ghost.name
=> 'Blinky'
Enter fullscreen mode Exit fullscreen mode

How? When you define a class, you just make it inherit from the ActiveRecord class (specifically, one of ActiveRecord's sub-classes), like this:

class Ghost < ActiveRecord::Base
end
Enter fullscreen mode Exit fullscreen mode

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.

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.

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

EXAMPLE

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
Enter fullscreen mode Exit fullscreen mode

Pacman

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.

Top comments (0)