DEV Community


GameTracker Project - Third Time's a Charm

robotspacefish profile image Jess ・3 min read

While I was attending Flatiron School I created two versions of an app called GameTracker to keep track of my game library. The first was created with Ruby and Sinatra and used the IGDB API to pull game data. The second was created for the Ruby on Rails project assignment. For that version I didn't get to incorporate IGDB, but I added some other features the first version didn't have, such as notes. For each project we had a week to get it done and had to adhere to a specific set of instructions, and therefore I had to cut some things out that I would have liked to add. I decided I would create this app for the third time, the way I want it, and keep track of my progress in this blog series.

This time, instead of creating everything in the back-end, I'll be using React and Redux and creating a Rails API to handle my database manipulation and IGDB API calls.

I thought I could create fetch requests to IGDB on the front-end, but after some googling I believe if I do that there is no way to keep my secret key private. I think the way around this is to make a fetch request to my Rails API, which will then call out to IGDB.

It also seems that Twitch now owns the IGDB API, so connecting is not going to be as simple as it was before because I have to authenticate through Twitch using OAuth. More to come on this because I haven't gotten to that part yet.

So far I began planning my database schema. In the previous versions I had trouble figuring out the best way to set up my tables because I wanted a user to be able to own a game on multiple platforms, but I wanted a user's notes to be connected to the game. I created a table called owned_games which connected a user to a game, and also connected with the notes table. I created another table called owned_game_platforms which connected a user to a game_platform. For example, if I own Rocket League on PC, Switch, and Xbox One, the owned_games table would show my user_id and the game_id for Rocket League. The owned_games_platforms table would have multiple entries showing my user_id with the game_platform_id for Rocket League on each console I own. This wasn't the cleanest solution because I had to maintain multiple table associations, but I didn't have a lot of time and it got the job done so I went with it.

I knew there had to be a better way to do this, so I dove back into the Rails guides and found polymorphic associations 🤯.

Instead of platform, I decided to call my table console, known as platform for polymorphic relationships.

class Console < ApplicationRecord
  belongs_to :platform, polymorphic: true

class Game < ApplicationRecord
  has_many :consoles, as: :platform, dependent: :destroy

class OwnedGame < ApplicationRecord
  belongs_to :user
  belongs_to :game

  has_many :consoles, as: :platform, dependent: :destroy

  has_many :notes
Enter fullscreen mode Exit fullscreen mode

Now a game can have a relationship with a platform and so can an owned_game, and I no longer need the extra owned_game_platforms table connecting a user to game_platform. So far I like this a lot , but we'll see how it goes after I get the rest of the database set up.

Discussion (0)

Editor guide