Automagically Inclined

Posted by jessenovotny on September 16, 2016

The week before starting my Rails project, was the most grueling and discouraging yet. We were covering authentication and we’re at the stage in our learning where training wheels are starting to come loose and the lesson material can get pretty thin. It all started with Omniauth which I couldn’t configure correctly, for the life of me (more on that in a minute). Before I could even grasp it completely, we dive head first into a steaming pile of Devise. This can be a frustrating section if you don’t have a solid handle on what is automatically generated with Devise and what isn’t. For example, I had just gotten used to the comfort of writing my own helpers like current_user and suddenly, all of that is made more me, along with the entire login/signup pages!!

Then we moved on to Pundit, which was actually pretty straight forward and rad! A great softener before “Project Mode” begins. I had a great experience working with another student to collaborate with on our Flatiron Store project. We started that Monday and had it knocked Tuesday night. Today is Thursday, my Rails app is finished and I’m pretty impressed with what I’ve been able to accomplish and what I’ve learned through solving my own problems. Creating this app from scratch was a real eye opener. I feel as though I flourished being able to go with the flow and decide all by myself where to devote my time and energy into what features I wanted and didn’t want.

My app is called “Questionable”. I based it off of Quora.com which is basically a massive Q n’ A forum. To be fair, there was a lot I couldn’t do with my current knowledge base, but I feel as though I really put my skills to the test. The first challenge was getting the Facebook login to work, so naturally I saved that for last.

I started by mapping out on paper all of my models and associations. We have Users, Questions, Answers, Upvotes, Categories, and QuestionCategories. The entire web of relationships was pretty simple except for Users/Answers/Upvotes. Users have Answers and Users can Upvote another User’s Answers so a User also have Upvotes from their “Upvoted” Answers. I had originally struggled with the notion of creating a column in Answers to store an integer for how many upvotes it had, but soon realized how silly this was. My next thought was to create a relation somthing along the lines of User has_many :upvotes, through: :answers but they already have many upvotes directly, and really the idea was to sort a Users answers by most upvoted. I ended up going with a straight forward instance method answers_sorted_by_upvotes which goes a little something like answers.sort{|a, b| b.upvotes.count <=> a.upvotes.count} which I read is much less expensive than using sort_by{|answer| answer.upvotes.count }.

From there, it wasn’t too much work setting up the MVC and routing infrastructure. There was definitely some trial and error finding the proper orientation of nested resources. One of the most consistent challenges was setting up my form_for and button_to helpers. There are simply so many options and configurations that I found myself referencing rails docs nearly every time I had to build one. I’ve definitely got the hang of it by now! One significant fact I learned is that you can only declare helper_method to define control methods for use in views within the ApplicationController. It took a little trouble shooting this one before I read this in StackOverflow.

All in all, I definitely felt comfortable navigating around most of my issues and only truly relied on the support of other students once or twice. Although it’s probably not the best approach, I didn’t really take the time to layout a map of what I wanted to build step by step. I had a picture in my head and had a general idea of how to put it together, so I just started creating pieces one after the other. It was only about half way thought the project that it felt necessary to outline what was left to do. It was about that time that I decided to tackle Omniauth.

After a while troubleshooting different issues and dealing with a particularly disdainful “unknown error” on Facebook’s end did I learn that when exporting your keys and secrets to the local environment that different Terminal tabs were in fact different environments all together which was quite possibly the source of my anguish from previous labs. I typically use 2-3 tabs when programming; one in which I am running the local server, another for bash where I run git commands, and maybe a third to run the rails console. The problem is that I would export my keys in one tab of Terminal and then try to connect with facebook through a server running in another tab. As it turns out, you need to export AND operate the server from the same environment and that different tabs are indeed different environments. This was likely the biggest relief and once I had Facebook running smoothly, everything else felt downhill.

From that point on, it was just a matter of building out the Category attribute assignment for Questions, generating the home page search filters, refactoring my code base, and moving large chunks of my views into partials. A couple of features which I’m most proud is the filter buttons on the home page which change dynamically based on what filter is being displayed and the display of all categories for a particular question as links that actually trigger a questions#update to remove said category. Until that point, there was actually no way to remove categories. This made things SUPER easy. Without leaving the questions/show page, one can now add, create, and remove categories with a couple clicks.

At the end of the day, I really wanted to take this as an opportunity to prove to myself and future employers what I was capable of doing in a short period of time and to be honest, I’m very proud of my accomplishment.