Sinatra Project: Associations Are Key

Photo by Joshua Aragon on Unsplash

While pondering ideas for a Sinatra application I contemplated the concepts covered in the Flatiron curriculum, picked out what I felt I needed a better understanding of, and wanted to focus on that for my project in the hopes I would improve. After all the best way to improve is to try, fail, and try again right? Looking back through the module at the Playlister lab in particular I had the most trouble in the section grasping ActiveRecord associations that were more complex than a ‘belongs_to’ and ‘has_many’ relationship between two models in a database. When there were more than two models to take into account I struggled with when and how to implement a ‘many-to-many’ relationship as well as ‘has_many through’ relationships.

I decided on an application that would keep track of a user’s movie collection, not only because of my personal attachment to the subject (I love movies and have a large collection), but because I wanted to be able to sort through the collection based on these factors: The name of the movie, the director of the movie, the genre the movie belongs to, and the format the movie is on. A user could look at all movies alphabetically or narrow it down to look at just the horror movies, or all movies in their collection by a certain director, or just the movies the user owned on dvd, etc. This required many models and I thought was a good way to push myself into understanding how to work with more complex relationships.

What I found helpful and recommend to those who may be in the same boat was to create the migrations and models I thought I needed, assign associations to them, and then go into tux and see how those associations worked. I spent at least a week creating data within tux so I could see how my different models associated with each other and what worked and what did not. This time proved invaluable because then I was able to associate movies with users and directors together as well as look at movies of a certain genre or format for one particular user. This allowed me to achieve the various sorting features I wanted my app to have in a fairly efficient manner.

Once I had the associations nailed down my work was cut out for me and I was able to move through the rest of the project with relative ease. If there’s one piece of advice I can give as a result of this project it is to find an area you feel you need improvement in, something that may hold you up from progressing on your project, and take the time to better understand it before diving in. It will hopefully prevent any confusion and stress once you are in the thick of it.

Industrial electrician working towards a career change in software engineering. Currently going through Flatiron School's online self-paced boot camp.