delight.fm: day 10

Persisting… a hash

Ryan Strickler
Little King: Daily Log
2 min readFeb 3, 2017

--

A couple days ago, I was stuck with an issue that I was having integrating some third-party code into my project. Happy to say I got unstuck! 🎉

I’m retrieving user details from Spotify using a gem called rspotify. Rspotify provides a hash of the necessary user information that you can store. Then you can recreate your user information and make additional calls to the Spotify API later as needed.

I was able to make API calls just fine when I first authenticated with Spotify, but I was having trouble reconstituting my user in a later method. I found the issue by creating a second user from the hash. When I realized that the second user object could connect to the API without issue, it was obvious that I wasn’t storing my hash correctly in the database.

The problem turned out to be that I was using the postgres hstore column type to store my user hash. hstore is a special data type for storing hashes, but the problem was that my hash contained objects that needed to be rebuilt. I switched my column type to text and added the serialize method to my class and it’s working well now.

Here’s the relevant code:

Don’t give up when you get stuck. Break the problem down and try to understand each part. Double-check your assumptions. Realize that there is always an answer to why your code is breaking and that if you’re persistent, you can find it.

--

--

Ryan Strickler
Little King: Daily Log

Building something every day. Launching something new every 6 weeks. Writing about everything along the way.