Step 2

Drew Pilgrim
Off by One
Published in
4 min readFeb 5, 2018

Second post on this blog details my experience working with Ruby on Rails again as part of my experience at Actualize. During an exercise in which we had to update and create additional functionality for a project we were working on. In this assignment I was stuck over multiple convoluted errors that seemed to not be related to the actual issue I had.

There was quite a lot of tasks assigned for this exercise which was mostly focused on database joining and many to many relations and how to create and manipulate them using Rails syntax. When I first was running into errors where it took me more than the usual “Check error message, notice bug and fix” minor issues that come with learning a new technology or syntax I was initially confused. In the project we were working on we were simulating an online store with products, in particular this exercise we were building the functionality for storing orders and building “Carts”. This would be similar to the checkout system at Amazon.com or other online stores.

The errors I were getting were somewhat confusing, as the syntax I was using was working on other parts of the project but failed in other areas despite it being exactly the same (To my eyes of course). Calling the has_many association with two models (products and carted_products in this instance) allows you to access an array of all the models one has associated with. In this case products.carted_products should return the array of all the carted products that contain that product. And this was working for product.carted_product. However, when trying the same syntax for user.carted_product the code would give me errors such as “The user has no method carted_product” despite the code working fine on the above.

After searching for a bit I realized my mistake with some helpful pointers from my professor. Due to previous functionality and design decisions for this project the associations were incorrect.

Anyone spending 5 seconds carefully looking over this code will notice a couple things. 1) The Order is both belonging to a user and has many users, 2) the order has many users and products through category_products not carted_products and 3) I didn’t even pluralize the users or carted products in line #4 and #5. I think this can point to a major problem I have with programming for large projects which is simply going too fast and not pausing to restructure my thoughts.

Even when initially writing my code I noticed the belongs_to: user and has_many :users and simply thought it was an odd way to write the syntax but thats all I did. Note it down as odd and simply continue programming. I should of immediately came back to that section that I didn’t understand and see if thats where my errors lied. I was planning on asking about that section after I had finished the exercise but since it compiled I assumed it didn’t actually influence anything. And while when learning a new subject you can’t always try to understand every aspect of every functionality fully and still progress at a reasonable pace. While first learning java you don’t really need to memorize that an int variable takes up 4 bytes as interesting as that may be. Still I should of focused and asked for immediate clarification.

Usually when learning something new one should try to focus on relevant information than just trying to learn everything about everything

As I was saying I think this is simply caused by me going to fast and more specifically going too fast when debugging. I don’t tend to make many mistakes when coding and don’t fall in the trap of writing a bunch of code then trying to debug the entire thing at once, once solving the above issue my code worked perfectly. But I think the issue is that I am used to solving small coding challenges like from online sites like Project Euler, Code Wars and problems from school which often times were difficult but mostly self contained.

I have had similar issues with Android such as when working on the Udactiy Nano Degree program I found myself struggling with a particular api JSON request and I was rushing through trying to find a fix as soon as possible. Rapid fire trying new fixes and patches without pausing to make sure my underlying understanding of the problem was correct. In the android case I learned I was parsing a bad request (I had an extra ‘?’ sign in there not knowing that uri method I was using was automatically adding a ‘?’ character to the string by default so my manual addition of a ‘?’ was causing the request to be invalid giving me wrong results despite passing in the correct parameters.

This seems like a pretty straightforward fix though. Next time I run into a non straight forward problem I will map out the process that I am working on in psuedo code. Then attempt to mark each step of that process and see if each step is working correctly and then make sure that the underlying assumptions I am making about the code are correct. While not the quickest way to debug hopefully this will be a more reliable way and save me time when faced with difficult and challenging problems like what I will face in my future career.

--

--