An Easy-to-Understand Example to Explain Object Relationships in Ruby
By: Sara Fotros
Tags: Flatiron School, Ruby, Many To Many Relationship, Programming, Object Oriented

Objects Relationships

In the first week of my course at Flatiron School, I have learned three types of relationships that objects can have with each other: ‘one-to-one’, ‘one-to-many’, and ‘many-to-many’. As an example of ‘many-to-many’ relationships, let’s begin with the idea of establishing a music band that wants to perform in a concert hall, and in order to promote your gig, you have to print posters or create a Facebook event.

Photo by Tim Bennett on Unsplash

As a “band” you need to have a name, the number of band members, as well as the number of songs you want to perform. Just imagine that we have to register our band so that we should fill out a form on a platform and then our band is officially registered. Think of this form(template) as a ‘Class’ in Ruby, which is a template to create or initialize an object, it can also have methods to describe behaviors and attributes of the instance of the Object it creates.

We also save all of the information in one form📋 (aka @@all) so we have access to all the information 📄 about the band.

class Band @@all = [] def initialize (name,member, song)

@name = name

@song = song

@member = member

@@all << self

end

end

so each band uses this platform or template, to register their bands (initialize). For now, let’s imagine there are 3 bands with their names, number of their band members, and songs registered.

band1 = Band.new("Ajam", 5, 10)

band2 = Band.new("Jaded", 3 , 12)

band3 = Band.new("Cold play", 6, 11)

On the other hand, in order to register(create/initialize) a place to be known as a Concert Hall or Venue, we use another template or Class to register it with some data such as name and location. let’s say there are 2 concert halls in our city where bands can perform there.

class ConcertHall @@all = [] def initialize(title, location)

@title = title

@location = location

@@all << all

end end

hall1 = ConcertHall.new("Town Hall","West London")

hall2 = ConcertHall.new("Cargo","City Center")

Now, each band wants to book some gigs on different dates and in order to promote their gigs, they need to print a poster or create an event on Facebook providing all of the information about their upcoming event. So we have another Class to create a Facebook event with the name of the band which is linked to the Band who has created it.

We can imagine that we have taged the band and venue on the events page which in this case means by clicking on the name of the band we will be able to go to the band page and see the number of the songs they are going to perform and band members, the date of the concert, ticket price, and name of the concert hall. and again if we click on the Venue or Concert hall, it will give us the location of the venue.

Therefore, it is easy to find @@all the information we might need through a Facebook event. For example, “band1” wants to know the dates of its own(self) concerts. The band leader can go to @@all the ‘events’ page and ‘select’ the events that the name of the band matches to itself (band.name == self). And then convert (map)that piece of information into dates(map). Or maybe a concert hall wants to know the name of the band and the number of each band’s members in order to prepare the stage for them. The owner of the venue can go to events and filters the events that his name is on it(select) and then for each event which belongs to it, has access to the rest of the information. Moreover, there are a lot of “methods” to do whatever we want to do with the information(.sum, .max_by, .include?, …)

what I was trying to explain was about the relationship between different objects that are linked together through another object, and not about how to write the code in Ruby.