Setting up a join table in ruby (or "How I made a dumb mistake and failed my code challenge, sigh.")
Published: 2020-03-03 00:45:43

One of the most stressful aspects of a coding bootcamp is the code challenge. Code challenges are test, one for each module of the bootcamp, usually coming in the middle of the 2nd of three weeks in the module. They are given in the 2nd week so that any student who fails them can re take them the next week. So far we are 2 mods into my bootcamp and I have had to retake both of my code challenges. Needless to say, timed tests are not my strong suit. 

For the most recent coding challenge, my main issue was in my implementation of what are called join tables. A join table is a class in an ActiveRecord database that has the sole responsibility of tracking a relationship between two separate classes. For this challenge in particular, the join table was tasked with tracking the relationship between a super heroine and her various powers. Notice I said “various,” thats because one of the reasons for needing a join table is to track what is called a many to many relationship. In a many to many relationship, no one object is a sole owner of another object. Superheroines can have many different powers simultaneously, while a power does not belong exclusively to one super heroine, but can be possessed by any number of them. The join table allows us to save that information in such a way that we can access it from multiple directions, making it easy to find out all of the powers of a particular heroine while also allowing us to easily query the names of all of the different heroines that possess a certain power. 

The join table is a very useful tool and, unfortunately, one that I wasn’t quite ready to implement effectively for my code challenge. I was more than aware that I needed to find a way to associate my heroines and their powers, but what I attempted to do was to create the association within an individual object that would have to be instantiated simultaneously with the creation of each new heroine. This proved to be impossible, as each time I tried to query the various powers a super heroine had or to find a list of the assorted super heroines with a certain power I was met with a frustrating error. I could find no way to effectively create the instance of a heroine and the new object of the joining relationship within one method. I spent many precious minutes of my coding challenge staring at my screen attempting to figure out a way to make my relationships worth, and eventually had to give up and move on to another part of the challenge. 

Ultimately, the seeds of my failure were sewn in my attempt to create the relationship between a super heroine and her powers within a traditional ruby model. What I should have done was to generate a new join table by using the command line to generate a migration called “CreateHeroinePowers”. Within that migration, I could have generated a join table with all of its magical properties by using the command “create_join_table” followed by the two items you would like to join, written as symbols (for this project, the command looked like this: “create_join_table :heroines, :powers.) 

After my code challenge was over, I was able to implement the join table properly and , to my amazement and frustration, all of the relationships were suddenly working. I was able to successfully query all of the individual powers that a heroine possessed  by assigning said heroine to a variable (h1) and then calling h1.powers.