Alex BecianaHomepage

#6 OOP Series: An Intro to ORMs and ActiveRecord

In this series, I will go over the principles and foundations of object-oriented programming and some principles on databases.

Alex Beciana | Blog | #6 OOP Series: An Intro to ORMs and ActiveRecord

In this series, I will go over the principles and foundations of object-oriented programming and some principles on databases. I will be using Ruby because I feel comfortable in the language; however, these concepts with some minor changes in syntax can be translated to other object-oriented programming languages, like Java, Node.js, etc.

Hi all,

We're starting to get to the end of this series on object-oriented programming principles but I'm still going to continue blogging. I think the next blog series might be on either a strategy to work use to learn a new programming language or perhaps I'll do some one-off posts about full-stack applications.

Recap

In my last article, I wrote about our many-to-many relationship in a relational database. In this blog series, we have also talked about what a relational database is and how its purpose is to represent the relationships we see in the real world and want to emulate in an application.

In this blog post, I want to introduce ORMs (object-relational mapping) and a popular gem in Ruby, ActiveRecord.

What is an ORM?

In the simplest definition, an ORM is a way for us to connect to a database and create queries to access information using our programming language. ORMs can be found in every programming language. In Ruby, the most popular ORM is ActiveRecord (it's also a dependency in Ruby on Rails).

With an ORM, we can cut down on repetitive code, and if you're like me and dislike writing out SQL statements, this will help you.

ActiveRecord

ORMs, like ActiveRecord, help to handle a lot of the heavy lifting, repetitive methods, and relationship methods that we had to write. Remember, how much easier things became when we implemented the attr methods to our classes? You're about to get even happier.

As an example, using our classes from last time — Dog, Person, and Adoption. Last time, we said that an owner (person) can have many dogs and a dog can have many owners. The joiner class (our Single Source of Truth) that holds a reference to both classes is Adoption. Recall, I said that you technically look at it as two has-many/belongs-to relationships. Using ActiveRecord, we establish this as:

Person.rb

class Person
	has_many :adoptions
	has_many :dogs, through: :adoptions

end

Dog.rb

class Dog
	has_many :adoptions
	has_many :people, through: :adoptions
end

Adoption.rb

class Adoption
	belongs_to :person
	belongs_to :dog
end

Yup, the syntax is that easy. However, there is more to ActiveRecord than that. You can also find more ActiveRecord associations here.

Using ActiveRecord, we no longer need to create a self.all method or utilize the attr methods (some extra ActiveRecord magic).

The next step is to take a look at my previous blog post and GitHub repo documentation for the Ruby gem that I created, rcli_app. My gem unpacks into a turn-key Ruby command-line application with some helpful gems.

To get the most of your application, I suggest using this resource that I found helpful when I was creating my first two command-line applications.

If you're not planning on using Ruby, then I would suggest checking out StackOverflow and any other programming language community forums to help you get started.

Conclusion

This blog series has been fun and I hope to create other ones.

I'm still trying to think of other topics to write about

You can find me on LinkedIn and GitHub.