Dearg Dearg - 1 year ago 90
Ruby Question

`retrieve_connection': No connection pool for Model (ActiveRecord::ConnectionNotEstablished)

I have a simple Ruby script i use successfully locally to import a CSV file to my model. This works in development but fails in Production (Heroku)

I can't seem to get past the following error:

`retrieve_connection': No connection pool for Company (ActiveRecord::ConnectionNotEstablished)


require 'csv'
require_relative 'helpers'
require 'active_record'
require 'rails'

# Connect to database
ActiveRecord::Base.configurations[Rails.env] || Rails.application.config.database_configuration[Rails.env]

class Company < ActiveRecord::Base
print_memory_usage do
print_time_spent do

#open the file
filename = 'lib/assets/1/BasicCompanyData-2016-09-01-part1_5.csv'
# count how many rows?
line_count = `wc -l "#{filename}"`.strip.split(' ')[0].to_i
# Print that result
puts "#{line_count}"

count = 0

CSV.foreach(filename, headers: true) do |row|
Company.create! row.to_hash
count += 1
p "#{count}"


heroku run ruby open_csv_prod.rb

This results in the reported error above. I have no idea why Heroku would complain about a pool as this is listed when i check ActiveRecord::Base.configurations[Rails.env] in Heroku Rails console.

I should say i have tried this with the following database configuration but it also fails because heroku stores database information in [DATABASE_URL].

adapter: 'postgresql',
host: ENV['IP'],
username: ENV['USERNAME'],
password: ENV['PASSWORD'],
database: 'dbb_development'

It is possible i am trying to connect all wrong, if so i just need to write to my Company model in a ruby script executed in the terminal.

Answer Source

This is a longshot but, you said that heroku store the DB config info in a URL. The following snippet is from the rails docs


Maybe you can connect like this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download