What’s going on under the hood?

This has been a common theme throughout my time in the Flatiron School so far.

While learning software engineering there are many tools that we use which have a lot going on behind the scenes or under the hood that make them work for us.

When I started to learn coding the first language, I was exposed to was JavaScript. If you had asked me which language was best a few weeks ago I would have told you “JavaScript, end of story”. As we near the end of phase 3 (Ruby) I am slowly starting to realize that Ruby has replaced JavaScript as my favorite language. While Ruby is a lot friendlier than JavaScript in terms of small mistakes, that is not the reason it’s replaced JavaScript at the top of my list.

Establishing relationships in Ruby is important as they make everything easier for us, however, one downfall of having these relationships is the sheer amount of time it takes to set them up. While we had attr_accessor, attr_reader, and attr_writer to help us out, we were essentially on our own in terms of using these relationships in methods as we still had to set up a couple of helper methods to grab the data that we wanted to use.

Once we started learning Active Record, I felt like a huge load was taken off my back. Everything was just easier with Macros. In Active Record, the way we made sure that our classes knew about each other was through associations using the has_many, belongs_to, and has_many through macros. Now I’m sure there are probably a few more macros that are useful, these three help me accomplish everything that we are working on right now.

When we first learned about these macros we just accepted that they were magic. Our instructors consistently reminded us that magic does not exist (don’t tell the kids). They always told us that these Active Record Macros do a lot of what we had to do in Ruby under the hood for us so that we don’t have to. With the being said I figured that with all that these macros do for us it would be nice to really understand what they are doing for us and while doing this I realized that everyone should take the time to thank these macros by learning about what they do and not just using them (being used isn’t the best feeling in the world).

The macros that I mentioned above are called associations, which makes sense since they establish relationships between classes. We can build these associations using foreign keys and the previously mentioned macros (has_many, belongs_to, and has_ many through). We can use the has_many macro to say that a Teacher has many subjects and a subject belongs to a teacher. Going even further, we can say that a teacher has many students through a subject. With these few commands, we have established our relationships.

That was easy

But what’s going on here?

Reading from the Ruby on Rails documentation, these macros are doing something very similar to what we were doing before using the attr_methods. We are creating methods to establish relationships between our classes.

Now you may be asking yourself “Why should I look under the hood?”

To put it simply, knowing what’s going on under the hood of something like these Active Record macros allows us to focus on doing some cool things with the now associated classes that you have. Without knowing what sort of methods are created by the macros we are partially blind which can lead to some messy code. By learning exactly which methods are created under the hood we can get a better understanding of what they are doing for us and how we can build on them to create cleaner and more functional code.

Before getting started with Active Record macros I would suggest that everyone read the Ruby on Rails documentation.