Greenshift 032: Messing Around With Databases

in #greenshift6 years ago

So last time we had a plan? Cool. Today we're going to mess around with some databases and get some results. But first, we need a way to view our database. I mean I'm not sure about you, but I'm not very fluent in raw SQLite3. Luckily there are programs we can use to view databases.

I personally like SQLite Manager. It might not be the most aesthetic piece of code out there, but it gets the job done. Rails stores the development database in the db folder, so let's check out how things are doing so far in that file using SQLite Manager to visualize the data:


Greenshift 32-1.png

First off, it's empty. Which is good because we haven't put anything in there yet. If something was in there I would be worried. But you will notice that the columns that we assigned via migration are there! Also included are the id, created_at and updated_at attributes. The first one serves as a unique id to differentiate the records and the last two are different timestamps on when the record was created and last updated respectively.

So, let's fill that database. What do we have to work with right now?


Greenshift 32-2.png

So, it looks like we are creating an Post object that doesn't use the database and then returning that post. So, we're going to fix that by replacing this code with some spicy Rails code using stuff from the ActiveRecord library that allows us to treat database records in similar ways to objects:


Greenshift 32-3.png

So, we're going to the find_or_create_by! function which allows us to create a new record if one doesn't already exist within the database. This prevents us from storing duplicate entries within our database. We check if a post is already stored via using the permlink and then assign the author and title to the record if it doesn't exist. This code should store complete records if everything is working correctly and the relationship is valid.

Let's check SQLiteManager to check out the results:


Greenshift 32-4.png

Well, good news, we now have something in our database. We have five records, one for each of our relationships. But now the bad news. While we are able to get the permlink and title attributes, for some reason, the title is not being initialized somewhere in the code. Thus, every record returns null for the title attribute.

We also initialized a record that has null for everything. This is the result of the relationship not returning any results. Which means we have some debugging to do. But for now I say good night because I really could use some sleep right now.

Sort:  

This post has been just added as new item to timeline of Q-Filter on Steem Projects.

If you want to be notified about new updates from this project, register on Steem Projects and add Q-Filter to your favorite projects.

Coin Marketplace

STEEM 0.25
TRX 0.20
JST 0.037
BTC 96252.72
ETH 3559.97
USDT 1.00
SBD 3.75