Creation of my BookMarked Sinatra Project

Muhammad Ullah
4 min readJun 15, 2021

Hello, fellow reader as I take you on a journey to the creation of Sinatra Project, Bookmarked! If you have read my blog, Library of Mo, then you would know that my CLI project for Flatiron School was a library of books. For this Sinatra project, I want to go on a similar theme involving books and stories. At first, I want my project to be about Users being able to create their own short stories. A User would have many Stories and a Story would belong to a User, but the only problem I had was being able to create a third model that would have a ‘has many through’ relationship. That is when I decided to create my project on Reviewing Books.

3 Models and Relationships

Once I had my mindset on what I wanted my Sinatra Project to be on, the ideas kept flowing in and in even while I was at my job, but I knew I had to ground myself before I became disappointed with not fulfilling all of my ideas, so I started small and created 2 models of a User(has many reviews) and a Review(belongs to user). To get started I used the Corneal gem to create a new file where my project will be. Instead of creating everything from scratch, the Corneal gem creates all of the necessary files required for the Sinatra Project(such as config.ru, rakefile, evironment.rb, layout.erb, application controller, etc).

One principle that Phase 2 of Flatiron school taught was ‘Separation Of Concern’ which is separating files and classes into different sections. When the sections are separated then it makes it much easier for the programmer to locate bugs, keep bugs from affecting all files, and creates order.

Following this principle of ‘Seperation of Concern’ saved me a lot of headaches. I cannot count the number of mistakes I have made during this project but because of this one principle, it has helped me become a better programmer and improved my ability to solve problems. Without this principle, I probably would have not been able to finish the project, or would have taken me a lot longer than it already did.

S.O.C Principle

Following this principle of ‘Separation of Concern’ saved me a lot of headaches. I cannot count the number of mistakes I have made during this project but this one principle has helped me become a better programmer and improved my ability to solve problems. Without this principle, I probably would have not been able to finish the project, or would have taken me a lot longer than it already did.

Next came for Users being able to log in or sign up to create an account and write reviews. Through validations, a user must fill in the textboxes of username and password in order to log in or sign up. When it came down to the password, I used the gem BCrypt that works with has_secure_password which takes the password and salts it which means that it turns the string into a bunch of random data which it then hashes. When a User is logging in and types in their password, it salts the string, and using ‘.authtenticate’ sees if it matches the salted password in the database.

Once a User has created their account, then they are able to create a new review or edit/delete a previous review they have created. A User can only edit/delete their own reviews and this is possible by creating a private redirect_if_not_logged_in method created in the application controller because it is needed in the other controllers and is called in routes where a user can create, edit, and delete reviews. The second private method is the redirect_if_not_authorized method which is created only in the review controller because that is where it is only used since that is where a user can create, edit, and delete reviews.

Migration Table and Relationships

The reviews table is the joining table because it belongs to a user and belongs to a review, so it makes it easier for the object to track who it belongs to and connected to. Using the foreign key of users and books we can set it to user_id and book_id which is how the relationship book has many users through review and vice versa get connected.

My Biggest Obstacle

The biggest obstacle that I faced during this project was the has_many_through relationship, but not because of the objects connected, but how to access them and connect the views and controllers. The first thing I did was googling, but after scouring the internet for some time, I could not find an article nor video explaining the one thing I was confused about. Every article and video only explained the relationship. I also when back to the Phase 2 modules and I saw a section where it was titled for what I was looking for, or at least thought it did. Even that only explained the relationship, but not how it is used. So, using previous labs, and a lot of trial and error, I was able to find my answer and my code was working. Since my struggle to find an article explaining how to use the has_many_through relationship was a failure, maybe I can create another blog explaining that.

Final Thoughts

I went through a variety of emotions when creating this project such as frustration, overwhelmingness, feelings of failure, but most of all joy. Whatever frustrations I went through or negative feelings I had whether it was from the project or life, I can say with confidence that I am really enjoying this everlasting journey I am on. I even developed some obsessiveness for programming(which I won’t let it get out of hand). All in all, I cannot wait for the next phase, the next trials, the next failure, the next success as I will always be falling forward.

--

--