Ruby: ORM & ActiveRecord
I’ve been introduced to countless acronyms during my time at Flatiron School, each one seemingly more daunting than the last. After diving headfirst into ORM, or object relational mapping, it was clear that this was going to be an anxiety-provoking process to firmly grasp. However, if you are coding in an object-oriented language such as Ruby, being able to harness the full power of an ORM will not only simplify, but organize the usage of a relational database (such as SQLite, which is what I’m currently using).
What Does An ORM Do?
ORM is the technique of accessing a relational database using an object-oriented programming language. There’s no special magic to an ORM — it’s simply a manner in which to implement code that connects an OOP to a database; think of it as being a middle-man between a relational database and your OOP of choice. This process is referred to as “mapping” & can be described as the drawn connectors between the database and our language models:
What’s special about this process is that it allows us to connect a database table like a Ruby class, & the database rows like instances of a class. However, we’re not actually saving Ruby objects to the database. Instead, we take the individual attributes of a given instance & save them as a row in our table.
ORM Framework & OOP Interaction
Let’s talk about how ORM’s & an OOP, such as Ruby, interact. Below, I have manually created a table that I’d like my code to produce to list dog instances, along with their attributes:
Now, we can use the SQL function INSERT INTO to painstakingly hard-code the information into our table, one-by-one. This would be very inefficient:
It would be proper to write out our Dog class in our OOP so that in the instance more dogs are added to the list, we can write a couple class methods that will simplify/save us the trouble of writing the above many, many more times:
Thanks to our two class methods self.all & self.save, any new dog instance can now be “shoveled” (think: “pushed”) into our array denoted by our class variable “@@all” — cool! Yet, is there another method that could take some of this heavy-lifting off our shoulders?
Enter: ActiveRecord
ActiveRecord is an ORM framework , a Ruby gem containing an entire library of code. It’s one of the most widely-adopted among users of Ruby. Thanks to convention over configuration, it’s possible to write very little configuration when creating ActiveRecord models. What’s this mean? Well, for starters, it means writing far less SQL; additionally, it enables us to call SQL functions in a more intuitive manner. More so, it allows users to interact with data using CRUD methods you may already be familiar with. When a class inherits ActiveRecord::Base, your class has access to basic CRUD functions!
Get Matthew Bates’s stories in your inbox
Join Medium for free to get updates from this writer.
To begin, we’ll need to tell Active Record where the database is located it will be working with, via ActiveRecord::Base.establish_connection:
ActiveRecord::Base keeps it stored as a class variable at ActiveRecord::Base.connection. Below, the code in-between is simply creating a new table with our previously-established attributes for our Dogs class:
The final step is to tell your Ruby class to make use of Active Record’s built-in ORM methods. To do this, we make our class Dog a subclass of ActiveRecord::Base — this is known as Class inheritance:
It should be noted that it’s very important to remember to make your Class names singular (Dog) & your table names plural (dogs).
Every time we wanted to create a new dog instance, we just inherit those features from ActiveRecord; instead of our lines & lines of code above to instantiate & save a new dog, we are able to use ActiveRecords built-in create function to add a new dog row to the dogs table:
Pretty neat, right?
Wrapping Up
Having been introduced to ActiveRecord after learning ORM fundamentals was a breath of fresh air for me. Not only did it allow to streamline & reduce the amount of code I had to write, it’s also an incredibly powerful tool. For more information on the seemingly unlimited amount of possibilities of ActiveRecord, I highly recommend stopping by here to check it out!









