Johnnie Gonzalez
Published in

Johnnie Gonzalez

Code with The Flow…Relationships

So it’s pretty safe to say you’ve most likely conjured up a curiosity into the world of programming. Wondering how the world of tech is able to be so creative at an agile rate. That’s what it seems until you’ve successfully written your first line of code (“Hello World”)…ohh the familiarity. Then comes the…wait…where do I go from here? How am I supposed to achieve certain methods within my code to get the desired outcome? Especially having to find your way throughout data already given to you or one you might have collected yourself. If you’re agreeing with everything that I just stated…don’t worry you are NOT alone.

When I first started learning to write code (in Ruby) I quickly realized the complexity that it beholds. With such a steep learning curve at first it might seem hard to grasp...just like with anything new in life. Stepping out of your comfort zone can be the biggest success story to many. It challenged my thought process in ways I previously believed I have already mastered.

I think everybody in this country should learn how to program a computer, should learn a computer language, because it teaches you how to think.— Steve Jobs.

Of course with a numerous amount of studying, trial and error you soon realize that everything makes sense. Maybe not at first, but giving your mind the time to actually absorb the newly found information really is a great achievement in itself. After all, about only less than 1% of the world knows how to actually read and write code!

“I make this look good”- Will Smith / Men In Black

Sometimes we are given a problem and as humans we always want the answer right away. So we look at the screen trying to decipher what looks to be so complexed, it feels impossible. We tend to overthink what is given to us only to find out hours later that your life could have just been so much easier if you mapped out your process in pseudocode. Simply just making your way from point A to point Z. Even diagrams to map out your associations. Yes it requires logical thinking of course, but that is the magic in it all. I mean you can’t exactly close your eyes and in the blink of an eye find yourself on the beaches in Thailand...that would be ideal yes. It comes with planning beforehand.

That leads us to the object relationships and arrays / hashes consumed with data. I was given a task to complete during my studies. Faced with the challenge of how to effectively iterate through arrays of data to grasp certain information. I understood the concept but that was just it…the concept. Mind you, this is forcing you to really think more into how to get to where your destination lies…as opposed to just being there. In Ruby writing methods and using the proper enumerators helps along the search for your precious pieces of data. Highly recommend checking the Ruby doc out for Enumerables!!

To continue into the directional path of iterating through data we have to understand how to get there. Whether it be through object relationships or simple nested data, in which both can and will be the case. You have to diagram the relationships between the objects. In order to fully unravel what direction you need to take for the desired data. This is the case with one-to-one / has-many / and many-to-many relationships. Might sound confusing at first, but trust me it really relates to so many real world instances. So let’s say you have a one-to-one relationship between classes(data) ex: a Person and their Passport.

The association between them is one person is assigned to one passport and in return the passport is associated to one person. The person biologically has all this information about themselves. The passport has that information as well through the person. They belong to each other. One-to-one relationships are pretty straight forward and are the foundation of understanding what’s to come next. One-to-many relationships in (data) occur one step above one-to-one in terms of complexity. For example, let’s say you’re on this much needed vacation flight to Thailand…

You, the passenger, only have one flight so you “belong” to the flight, but the flight “has-many” passengers. They are closely related; only the flight in turn has all the information about “many” of its passengers. While the passenger only has information about that one specific flight (date, time, destination). No matter what they are still connected through a direct relationship. One clearly with more data than the other.

Now that leads us to our “many-to-many” relationships. Don’t fret! I promise to make this easy to understand!! Many-to-many is another elevated step between object(s) relationships that a programmer should fully clutch in their learning to code. I like to think of it as a flow of information through association to get another set of data from another non direct relationship. In the example under I will relate it to your trip to Thailand!! Oh so fun! We’ve just landed!!!

Let’s say you’re traveling in a group headed to Thailand. You get there of course your hotel will have rooms and the guests either have a room or share a room with someone in the group. The logic in the tables above might seem appropriate, but it’s missing what we call a “join” table. A join table holds the id’s of each object regarding both tables of data (Guests and Rooms). So we’ll name it “Hotel”.

Both Guests and Rooms have a relationship through the join table (Hotel). Both belonging to the “Hotel” join table. The join table “has-many” Guests and Rooms. In turn in order for a Guests to get the information regarding their Room they have to flow through the join table (Hotel) and access its data. Vice versa for the Rooms as well. This is where the “flow” of retrieving data comes in. You’d have to flow through one set of data(Hotel) to retrieve another because that’s the only relationship they have with each other. The join table being the set of data holding both information of id’s regarding the associations (Guests and Rooms) .

With much practice leads to better understanding. Trust me we’ve all been there. Just remember given the type of associations you have to go through. Go with the flow (how to get there). Map it out..diagram it so you’re not stuck thinking in the middle of your code..how? Although it’s inevitable.

SpongeBob

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Johnnie Gonzalez

Johnnie Gonzalez

23 Followers

Software Engineering creative with background in architectural design. Love for sustainable design and tech!