Reputation: 76774
--
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:
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
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