The fact that object orientated programing can in many way be used to model the behaviors and patterns of real world objects that we as humans interact with on a daily basis is fascinating! Recently at Flatiron School’s software engineering program, I had the pleasure of learning how to program in the Ruby language. It was during this time that I was first introduced to object orientated programming. And what I found so fascinating is that we can create object on our computers that behave similar to their real world examples.
What I learned very quickly is that the relationship of these objects were at its core design. Without these relationships (or associations), the objects themselves were pretty boring. I began looking world around me and realizing that the same holds true for many if not all things in life. If you take the time to look around the very room your sitting in right now, you have many objects that have some sort of association with each other or with you. An example might look like this…
- A song belongs_to an artists
- An artist has_many songs
- Songs has_many genres
- Genres has_many songs through artists
As you can see from the above graphic, even a simple relationship can quickly get complex depending on the real world example you are trying to emulate. Just imagine how complex these relationships can get with more and more objects. During my short time programming in Ruby, I learned that it is often beneficial to draw these associations on a piece of paper, a white board or even in your text editor when planning. Trust me on this. You do not what to be trouble shooting why you are unable to query some sort of data only to find out that you did not properly associate all your model relationships correctly. Taking the small amount of time to draw out your associations will save you time later. Especially if you have a complex project with many associations. A quick tip we learned in times code challenges during boot camp was to just quickly draw your associations right inside your test editor like this…
In the image above pay attention to the “less than” or “greater than” symbols.
The symbol in this orientation — < means that the model to the left has many of the model on the right and the model on the right belongs to the model left .
The symbol in this orientation > — means the model on the right has many of the model on the left and the model on the left belongs to the model on the right
Common mistakes made during associations include but are not limited to …
- The use of singular or plural in your associations. Read your associations out loud to yourself. “Artist has many songs” should not read “artist has many song” (notice how the plural version is more natural when you speak it out loud?).
- Colon in the wrong location. This is usually made in the very beginning but should quickly become second nature with a little practice.
Any mistakes in your model associations will result in your models not being able to access information from one model to the other. So double and triple check your associations.
As soon as you make all the correct associations in your models (assuming you have seed data in your database) immediately open up your console and begin testing Active record queries like Author.books to see if your can access the information from that database. The run Book.author so see if that relationship is running correctly. You should be able to access the other model’s information. These methods are provided to you by Active Record just by the simple associations you made at the top of you models.
Final word of advice on Active Record relations that usually trips beginners up. And that is the concept of foreign keys. If any one of your objects has a belongs_to relation to another model, you will need to include a foreign key on that models table. So for instance, in the example below. The Book model belongs to both an Author as well as a Genre. Because of this the table in your database will need to have two foreign keys. One for the author_id and a second for the genre_id. Without these foreign keys, you will not be able to make proper associations in Active Record.
In conclusion, once you make all the proper associations its pretty amazing what Active Record and Ruby can do in terms of accessing your data on a database. The use of object relations and associations is a core component of object orientated programming. Without the associations we would just have boring models. While it can be confusing at first, stick with it and it will start to be come second nature.