The problem with relationships…. In Ruby.
Here we are, I’m on a new path to become a software developer. It was smooth sailing until my classmates and I realized we would struggle with the same thing developers and non developers struggles with…relationships.
Relationships, yes relationships. Not ones we are used to, like fighting with your partner about chores, instead these ones are a bit more complex.
So what are Relationships exactly ? According to google dictionary it is the way in which two or more concepts, objects, or people are connected, or the state of being connected.
In mathematics and statistics it is the correlation or dependence between two variables or set of data. So what happens when we bring this to computer science? Techopedia.com defines relationship in the context of databases as:
A situation that exists between two relational database tables when one table has a foreign key that references the primary key of the other table. Relationships allow relational databases to split and store data in different tables, while linking disparate data items.
Ok so we understand the definition of a relationship. Now, in order to talk about relationships in Ruby we need to understand two principles of object oriented programming:
- Polymorphism which is the ability for data to be represented as many different types. “Poly” stands for “many” and “morph” stands for “forms”. OOP gives us flexibility in using pre-written code for new purposes.
- The concept of inheritance is used in Ruby where a class inherits the behaviors of another class, referred to as the superclass. This gives Ruby programmers the power to define basic classes with large reusability and smaller subclasses for more fine-grained, detailed behaviors.
In Ruby there is something called Object Relationships that contextualizes Classes.A class in ruby always starts with the keyword class followed by the name of the class. The name should always have the first letter capitalized. Has many and Belongs to sets a connection many to many between classes in Ruby.
These classes often have three main parts. An initialize method, an instance method, and class method. Lets talk about their differences and how a better understanding of them can make our life much easier.
This method gets called when the object has been created by the class method. Whenever you call .new in a Class it will create a new instance of itself.
That is not hard to understand, every time that we create something it’ll initialize a new object with some attributes.
The real struggle comes when we have to work with class methods and instance methods, what immediately comes up is the keyword self.
Every object knows itself, in every method and inside any method the object can be referred to using the keyword self.
Before we dive deeper into self, let’s move on to the Class Method.
A class method belongs to the class as a whole, its not tied to any particular instance, this method is always prepend with the keyword self.
In this particular case we have two class methods, the first one is taking a class variable and counting its length, and the second one is turning that variable into an empty array.
We have to call the class itself in order to access to those class methods.
On the other hand we use instance methods when we need to do something in a particular instance of the class, it requires an object in order to call it.
We cannot call these methods as we did with the Owner.reset_all in our class Method example, since these two methods are not prepended by a self keyword it means that they require an object to call it.
So how we do it ?
We have to create an instance of our class and call these methods in them.
Now that we have our objects(instances) we can call the method in it.
Now we can summarize the main difference between these methods and it relates to self:
- self, inside of an instance method, references the instance (object) that called the method - the calling object.
- self, outside of an instance method, references the class and can be used to define class methods.
Although all relationships are complicated in Ruby, if you focus in understanding the three main methods (initialize, class, instance) it should make it easy.
Before writing any code, make sure you understand what the relationship is, and draw a basic diagram for it.