“Active Record Associations and Me: This Sort of Thing Is My Bag, Baby”

Anthony Rubbo
The Startup
Published in
4 min readNov 8, 2020

--

I began my coding journey in my ::cough:: mid-thirties after spending my teens and twenties honing my guitar-playing skills to become a professional musician. Upon some reflection, along with a little push from my friend and yours, COVID-19, I decided I needed a new direction. I realized, through the advice of fellow musician-turned-SE friends, that I found a similar sense of satisfaction from coding as I did from performing.

I put in the maximum effort to get accepted to coding bootcamp and found it to be one of the most challenging and rewarding experiences of my adult life. I was on top of the tech-world! — Until we began Active Record (AR). For some reason, the notion of “association” was confounding to me — even though I had understood that notion when learning object-oriented programming! I thought attr’s accomplished that job for us??

It wasn’t until the fact that AR was just a Ruby gem — a collection of code that someone else has written — sank in that I began to understand it’s true usage. AR simply allows us to use Ruby code to manipulate database information without having to write out, in this rookie’s opinion, much more complicated and verbose SQL code. AR is pretty CRUD-y (yeah, I went for that joke), except it’s not. CRUD is an acronym for actions used to manipulate data within a table; Create, Read, Update, and Delete.

Those attr’s I was used to writing out to form associations between classes with object-oriented programming are encapsulated into AR macros. The most useful among the bunch being belongs_to, has_many, and has_many_through. These macros allow us to perform CRUD actions by giving us access to useful methods that already exist!

Detailing the steps of association through something I knew intimately — music — helped me to solidify associations. I’m a musician and I have many instruments (too many, some would say). I also have many gigs (in a perfect world), through my ownership of my instruments. This details has many and has many through associations. My instruments belong to me, but the gig shares ownership (theoretically) of the instruments making up the band. This has many / has many through association functions in the other direction as well, making Instrument the “joiner class”. Translating these associations into code is as easy as 1–2–3.

I now have access to methods through AR that I would have had to manually code prior. For example, I can create new instances of musicians with #Musician.create, allowing me to accomplish the task of #.new and #.save (to the database) at the same time. I can also chain-build associations. By associating the “child” instrument to the “parent” musician, I could create instances of musicians already associated with particular instruments through #Musician.instruments.create. Let’s say I want to see all of the musicians I have available to hire— .Musician.all already exists without having to declare @@all = self upon initialization. Speaking of initialization, we’ve already accomplished that as well! Want to fire the entire band on a gig?! #Gig.musicians.destroy_all will accomplish that in an instance.

A more complete guide of instance and class methods, along with much more association information, can be found here and here.

Finding out more about what methods become available through the use of AR macros is part of the journey to becoming a more efficient coder. “Writing clean code” is something I find more and more to be part of the ethos of software engineering. AR and it’s means of association provide clarity and brevity in code. This allows others, whether colleagues on a project, or others viewing your code for the first time, to interpret your ideas clearly without leaving it to the imagination.

AR macros also save time and typing. Chain building instances of objects with attributes already filled upon initialization allows you to spend more time being creative with what you do and create, not how it’s done. As a musician, I was always and continue to be fascinated with how things worked — why does this scale work over this chord? What actually makes a chord minor, or major, other than sounding happy or sad? With Active Record, I find myself less concerned how code functions and more so that it just does. Active Record is beautifully written code (a gem) that lets me write more beautiful code — and that’s all there is to it.

--

--

Anthony Rubbo
The Startup

Recent Flatiron School Grad, hopeful SE in search of a job!