DEV Community

Sid Saythongphet
Sid Saythongphet

Posted on

Third Project Blog

In this post we will be talking about what got me through my third project at the Flatiron School. Before starting on this phase, my coding knowledge was pretty limited to the frontend. I enjoyed my time with JavaScript and even more so with React. I've found myself truly engaged in the process of learning how to code and my interest continued to grow even more during this phase as we delve into the backend. We were starting to learn a new language, and that language was Ruby.

So what is Ruby?

Ruby is...
A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
Source: https://www.ruby-lang.org/en/

For me, I enjoyed Ruby a lot. The code looked cleaner and the syntax seemed easier to read. At first, I thought I would dread not being able to see how my code was working in a browser, but I found that I actually enjoyed just having the tools to just run my code in my terminal. It also gave me a different incite on debugging. To be honest, I am not particularly fond of the debugger tool in JavaScript, and I mostly just added a console.log in whenever I needed to check stuff out. However, with Ruby's "Pry" gem, I quickly made a new best friend.

Pry is a runtime developer console and IRB alternative with powerful introspection capabilities. Pry aims to be more than an IRB replacement. It is an attempt to bring REPL driven programming to the Ruby language.
Source: https://github.com/pry/pry

I found myself using “binding.pry” a lot in my project. It helped me in creating my models, figuring out how to generate my seed data, and establishing my routes.

One example was when I was trying to find a join table instance based on the ids of its two belongs to tables. In doing so I first created a get route to play around with the code.

Get ‘/users_clubs/user_:user_id/club_:club_id’ do
binding.pry 
end
Enter fullscreen mode Exit fullscreen mode

I then booted up my Postman and made a request to the address. This then opened up a repl for me to check what my params where. I had my params and I went to figuring out the proper syntax to find my UserClub instance where both the user_id and club_id matched my params. After a few tries, I found ...

[2] pry(main)> UserClub.where(['user_id=? and club_id=?', 1, 1])
D, [2022-02-27T15:31:55.728583 #1719] DEBUG -- :   UserClub Load (3.8ms)  SELECT "user_clubs".* FROM "user_clubs" WHERE (user_id=1 and club_id=1)
=> [#<UserClub:0x000055a72e43ca58 id: 11, user_id: 1, club_id: 1, created_at: 2022-02-25 17:45:26.001501 UTC, updated_at: 2022-02-25 17:45:26.001501 UTC>]
[3] pry(main)> 
Enter fullscreen mode Exit fullscreen mode

I found what I was looking for, however it still wasn't coming out just right for my request. I continued to figure out what I was missing and it turned out to be that it was returning an array, which meant I needed to pull just one index of the array. so I adjusted my code UserClub.where(['user_id=? and club_id=?', params[:user_id], params[:club_id]])[0] and successfully got my delete route to work.

delete '/users_clubs/user_:user_id/club_:club_id' do
    @user_club = UserClub.where(['user_id=? and club_id=?', params[:user_id], params[:club_id]])[0]
    @user_club.destroy
    @user_club.to_json(include: [:user, :club])
end
Enter fullscreen mode Exit fullscreen mode

This continued for a lot of my progress during this project and made the process of finding out how everything could connect fun.

Top comments (0)