Object Relationship in Ruby
By: Hanna Mulugeta
Tags: Flatiron School, Oop Concepts, Ruby, Ruby on Rails, Software Engineering

My Journey of becoming Software Engineer has started a few months ago when I decided to join Flatiron School’s full-time immersive program. Before I joined though, there was a pre work that I was required to complete. It covers the basic and foundational skills of any programming language. It starts by discussing scalar data types like integers, strings, boolean values, … including the way to manipulate them, and extends to up to collection data types like arrays and hashes.

It’s very important for anyone, to take time and really understand the concepts as it will help a-lot on the way forward. Once joined, I got introduced to Object Oriented programming language. And in this blog, I will talk about what it is and how one of it’s relationship works.

Object Oriented Programming(OOP) language is a language that enables us to represent anything as an object. Think of anything that exists in real world, it’s considered as an object in OOP and it can be created using the concept of Classes. Classes allow us to define and create real world objects, by incorporating their behaviors and functionality.

When we see objects, they can’t exit by themselves, but rather interact with other objects in one way or another. As programmers our job is to map and reflect the real world interaction into our code to solve any problems.

In this article we are going to see two Object Relationships in Ruby programming language. The Has Many/Belongs to and Many to Many Relationship

Has Many/ Belongs to Relationship

Let us take two class examples, a Song and an Artist. And let’s assume that a Song belongs to one Artist but an Artist in fact can have many Songs.

We can show this relationship using the below Entity Relationship diagram.

But how can we show this relationship using codes. Its has got two easy steps.

Step I — Belongs To

In the Song class putting a setter (attr_writer) and a getter (attr_reader) methods of the artist or simply attr-accessor to indicate both, will automatically create the belongs to relationship.

So whenever we create an instance of the song class, we need to mention who the artist is, by assigning the artist’s instance, not his name(String Value)

Step II — Has Many Relationship

The right way to indicate an artist class has many songs, is by declaring a class variable (@@all) in the song class and set to an empty array. This will enable us to capture every instance of the song upon initialization. Note that through the belongs to relationship we can know who the artist of each song is.

This implementation will maintain a single source of truth, instead of capturing data in two classes we just did in only one place(Song class) with out loosing any functionality.

The established relationship will help the artist to know all of his songs, by using the songs method, how awesome is that!

Whether we create a new instance of the song, using Song.new( ) or using add_song_by_name method under artist class, it will always pushed to the Song class variable @@all and an instance of the artist is also assigned to the song. … to be continued