day 10

Persisting… a hash

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:

# controllers/sessions_controller.rb
class SessionsController < ApplicationController
def create
spotify_user =['omniauth.auth'])
user = User.find_or_create_from_spotify(spotify_user)

# models/user.rb
class User < ApplicationRecord
serialize :rspotify_hash, Hash
  def update_albums
spotify_user =
albums = spotify_user.saved_albums
# it works!
  def self.find_or_create_from_spotify(spotify_user)
user = find_or_initialize_by(spotify_id:
user.update_attributes!(rspotify_hash: spotify_user.to_hash)

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.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.