Richard Peck
Richard Peck

Reputation: 76774

Dynamically Adding To database.yml

multi-tenant ROR app

--

I'm trying to figure out how to call different databases from database.yml:

#config/database.yml
#Defaults
default: &default
  adapter: mysql2
  pool: 5
  timeout: 5000

dev: &dev
  <<: *default
  username: *****
  password: *****
  host: ******

##########################

#Admin
development_admin:
  <<: *dev
  database: *******

##########################

I'd like to make the database key a variable, so it can be set like this:

class Admin < ActiveRecord::Base
  self.abstract_class = true 
  establish_connection("#{Rails.env}_admin", database: "our_db")
end

The problem is an error occurs as follows:

enter image description here


I'm sure there's a way to fix it - I'm interested to hear if anyone has any method to fix the issue?

Thanks!

Upvotes: 1

Views: 1148

Answers (1)

Vakiliy
Vakiliy

Reputation: 871

If correctly understood the question:

con = Rails.configuration.database_configuration["#{Rails.env}_admin"]
establish_connection(con.merge('database' => 'our_db'))

Or, if simple use configuration:

establish_connection("#{Rails.env}_admin")

Upvotes: 3

Related Questions