BookShare: My first Sinatra based web application.

Zeshan Raja
2 min readAug 16, 2020

--

It was a dark and stormy night when I decided to make an app about books. As a recreational reader, I figured that doing a project about something that I am passionate about would make this a bit easier. In my head, the execution seemed simple enough; a user can view a library, add books from that library to their favorites, and edit/delete them at their leisure. As most endeavors that seemed simple, it turned out to be anything but.

I’ll break this down into a three-step process: Preparation, development, and refactoring. As someone who likes to pride himself at being amply prepared for stuff, I took my time mapping out my ERD. A user has many books, and a book belongs to a user. A user should be able to view books and to add them to their favorites. The beginning of the development stage actually went pretty smoothly. A flatiron instructor made a series of youtube videos building a web app from scratch, which proved to be an indispensable resource. The problem, however, came when I tried to create new instances of my Book model.

As it turns out and as much as I’m ashamed to admit, I was ill-prepared. Creating a Book instance did the job of actually creating a book instance with the User ID, but it then created redundancies in my database. So when a user viewed their favorites, 1984 would appear like a dozen times. So I talked this out with my brother who sorted of stated the obvious: If I wanted to have the user add to favorites, why don’t I have a favorites table? Why not, indeed. So I started from scratch and added that favorites table.

Then it was just a matter of implementing CRUD on the favorites model. One challenge that I did encounter was because of the fact that I offer to add (see create) more than one favorite at a time, those new favorites were ids in an array that I had to iterate through to create/update from. The upside was that this data, the list of favorites, is not accessible to anyone but the current user, so I didn’t need to worry about other users tampering with other users resources. Another challenge my delete method. Because I offered a checklist where multiple favorites could be selected, I was not sure how to do that without first making a get method that rendered a delete ERB page that held a form that then points to a delete method (see, I’m tired from just typing all that). A classmate of mine then suggested to offer a link to view a certain favorite (dynamic links were something I was then missing), that led to a favorites page where you would display the favorite and offer to either edit or delete it. Again, simplicity escaped me.

For the next project, I would consider what I missed this time, creating more models if need be while not getting too carried away with over ambition. Overall, a positive experience.

--

--

Zeshan Raja

A beginner student of programming, a beginner student of life, a beginner at pretty much everything.