Object Relationships

Franchell Polanco
The Startup
Published in
5 min readJun 8, 2020

--

A Beginners Guide to Object Relationships in Ruby

Assuming you have learned how to build a standalone class. You are now discovering how to make different objects relate to each other. If you are a beginner programmer you have to wrap your head around making these objects relate in your program, envisioning them as real life objects. This blog is offered with the intention of bringing you ease in wrapping your head around this.

Object Oriented Programming (OOP)

In object oriented programming we model real world situations and environments. In each, the objects or models are representing one thing but we have to model how they relate to each other. The relationships work abstractly and we have to express them in the program design. For a more in depth look into OOP check out this documentation.

Let’s use the example of a basic music application that lists out artist with their corresponding albums to show how the relationships work abstractly and how code works to build these relationships in our program. As we create the models they will have a relationship with each other to interact and add functionality to our application. For the sake of simplicity we will focus on just two models (Artist and Album) and how they relate to each other.

Lets say we have an artist, and more than likely that artist has more than one album. That artist has_manyalbums. Let’s use Rihanna as our example artist. While R9 is a highly anticipated album by fans, she currently has 8 albums. Let’s see how Rihanna and her albums are related in the following diagram.

Made with Figma.com

I recommend a visual diagram to actually see the relationship between your objects, especially for more complex modeling where you have three or more models. This now helps us organize the relationships by differentiating the levels of relationships. Aside from albums, our Artist has a name, age and hometown. Initially the class would be defined like this:

Each class will have its own attributes that pertain to this specific class.The Artist would not be able to exist without these attributes. Therefore Rihanna is an instance of the Artist class with the attributes of name, age and hometown. To create this instance we could seed the data.

The next level or class is our Album class, in the diagram we can see all the albums that belongs_tothe Artist class. Albums belong to an Artist and these eight particular album instances belong to an instance of the artist class(Rihanna). Since the albums are not listed in the instance of the artist how will our program know the albums that belong to Rihanna?

This code example includes macros for our setter and getter methods for each instance

Now every time an artist is initialized the albums array is a place to hold each album object. Not just the name of the albums but the album object with all its attributes. This begins to add functionality to our program by having the Artist class communicate with the Album class. At this point in our code Rihanna doesn’t have any albums yet.Let’s head to our Album class:

Our Album class has an artist attribute in the macro method taking care of our reader and writer methods. Notice how our artist instance was not included in our initialize method? Let’s consider how the objects relate to one another to figure out why.

What exactly is artist in this Album class? It is the particular instance of the Artist class that currently owns the given album object. Excluding artist from our initialize method allows us to add R9 when she finally releases it. How do we add R9 to the Rihanna artist instance when it drops? We include an add_albummethod in our Artist class:

Here our add_album method takes the argument album. This is the album object. Make sure to pass in the object here, not the album title. Think of it like this you want to add the actual album to Rihanna’s collection not just the title. We enjoy the album by listening to it, not just staring at its title. On the second line @album is our empty array in this Artist class, when we receive the album we want to store it safely. After all that hard work we don’t want our album leaked right?

The last line has the self concept which can take a second to grasp, let’s break this line down. album is the object passed in the argument, artist is the uninitialized setter getter method in our Album class because Rihanna didn’t finish up the paperwork to add this album to her collection just yet. In this line, through this method, we are saying Rihanna has successfully added this album to her collection. So what is self here?

In plain english self in this line would be described as such; Let’s say album.artist in plain english is written as “ANTI is added to Rihanna’s album collection” the ANTI album is the object on which the “added”action is happening on. So if I write “She’s proud of it” we know “it” is referring to the ANTI album. In English action verbs act on direct objects, in code methods act on objects. Self is similar to “it”. Basically, self is the object receiving the method call. Since we are in the Artist class self is Rihanna, it’s like Rihanna is saying lets add this album to my collection.

Even though this is happening in an instance method in the Artist class, the actual claim of the album to the collection is labeled on the album object. The album array isn’t keeping track of it’s albums even though it is storing them. The album object needs to keep record of the artist to which it belongs.

We can use the terminal to create our instances and see if our method works. If it does, congratulations! We now know the basics of building a has-many and belongs-to relationship between two objects in Ruby.

If we wanted to explore a many to many relationship with this example we can think of a song like “Run This Town” which has manyartists (Rihanna, Jay Z and Kanye West) and these artists have many songs.

As programmers it is crucial to understand these relationships to effeciently design our programs.Luckily, Active Record saves us time to write way less code than what you saw in the above examples. To get a look into Active Record Associations check out this documentation.

--

--