0

I am trying to setup connection to another DB

class Something < ApplicationRecord
  self.abstract_class = true
  establish_connection {adapter:"postgresql",host:"localhost",port:5432,database:"dev_x",user:"user",password:"1234"}
end

But whenever I try to run a method on (e.g.: Something.all) it I receive error:

no implicit conversion of nil into String

Any idea why?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
knagode
  • 5,816
  • 5
  • 49
  • 65

2 Answers2

1

Your problem is that Something is an abstract class:

class Something < ApplicationRecord
  self.abstract_class = true # <-------------------------

Abstract classes aren't mean to be instantiated directly, you're supposed to subclass them and instantiate the subclasses. The abstract_class property is, more or less, a way to subclass models without invoking STI (Single Table Inheritance).

Either use Something as a base class for models in the second database or remove the self.abstract_class = true to make it a "real" model class.


As far as where your no implicit conversion of nil into String error comes from, keep in mind that abstract model classes don't have table names and cannot be instantiated, from the documentation:

class Shape < ActiveRecord::Base
  self.abstract_class = true
end
Polygon = Class.new(Shape)
Square = Class.new(Polygon)

Shape.table_name   # => nil
Polygon.table_name # => "polygons"
Square.table_name  # => "polygons"
Shape.create!      # => NotImplementedError: Shape is an abstract class and cannot be instantiated.
mu is too short
  • 426,620
  • 70
  • 833
  • 800
1

Worth mentioning why I didn't use code properly. We are also using Octopus gem (gem for running multiple database). Native establish_connection method doesn't seem to work when Octopus is turned on :(.

For all of you out there - use octopus_establish_connection but beware: https://github.com/thiagopradi/octopus/issues/101

knagode
  • 5,816
  • 5
  • 49
  • 65