Search code examples
ruby-on-railsherokuredisredistogo

Redis works on console, but fails in application


I'm using Redis with split gem in a RoR application hosted on Heroku.

I've configured it with RedisToGo using the following codes:

/config/initializers/redis.rb

uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/" )
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

/config/application.rb

config.gem 'redis' 

When I try to

REDIS.set("foo","bar")

on Heroku console, it works fine. It shows Redis ToGo address.

However, when I try to load the application I get the following error:

Errno::ECONNREFUSED: Connection refused - Unable to connect to Redis on localhost:6379

Howcome REDIS is responding correctly, with correct address in Heroku console, but it shows localhost address when the application calls it?


Solution

  • I was able to fix it, and I'll let the solution registered:

    I wasn't initializing Split.redis, and therefore it was trying to create a default Redis, which has localhost as host.

    So I created the following initializer

    /config/initializers/split.rb

    Split.redis = REDIS
    

    and then Split could find it!