Chapter 2: Relationships

Posted by emorgan05 on October 5, 2017

I was working my way through Object Oriented Ruby on Saturday, and found myself immersed in Collaborating Objects. Collaborating Objects is all about the relationships between Object classes. We start with the belongs_to, then has_many, then has_many_through.

But before I could get very far into all that learning, I hit a major issue. I was trying to create an MP3 Importer that worked with a Song class and an Artist class.

After working with Dakota for more than an hour, we got my code working. Instead of rushing off to rescue the next person, Dakota spent a little longer talking to me. He explained that these relationships are one of the hardest concepts to work with, and figuring out how to work the relationships is a major challenge to tackle. His advice was to create diagrams. He said before you start trying to write anything, any code at all, create a diagram that shows the relationships you are working with.

He suggested a couple of websites as options to build the diagrams, but I know it helps me to write things down with a pen on paper (I may be a little more old school than I should admit. If I call it hipster is it better?). So that is what I did. I took each lab and mapped the relationships on paper. I drew how the classes connect, and then I drew what methods they needed and how those methods linked.

I crammed my diagrams onto pieces of paper, but I could have filled a wall-sized whiteboard too. As I wrote the actual code, I kept going back to my diagrams, and it clicked. I knew what I needed to be doing, and I had some idea of how to do it.

My takeaway: All of this goes a lot more smoothly if I understand the big picture of what I am trying to do. And I think the biggest thing I am learning here is how to think about how to create something in code. The diagrams help, asking questions like Avi does helps, and it’s all just a shift in how I approach thinking about a problem. Both of those strategies help me slow down and work through each step in the process.