Reputation: 6622
I created a class which connects to 2 databases:
class Production < ActiveRecord::Base
ActiveRecord::Base.establish_connection(:production)
self.abstract_class = true
attr_accessor
end
class Backup < ActiveRecord::Base
ActiveRecord::Base.establish_connection(:backup)
self.abstract_class = true
attr_accessor
end
class RepairReport
def init
end
def repair_now
Production.table_name = "users"
users = Production.all
users.each do |user|
puts "USER: #{user.last_name}"
end
end
end
This is the database.yml
:
production:
adapter: postgresql
encoding: unicode
database: test_eagle_development
host: localhost
pool: 296
username: postgres
password: password1
template: template0
backup:
adapter: postgresql
encoding: unicode
database: test_eagle_test
host: localhost
pool: 5
username: postgres
password: password1
template: template0
When I run this, it gives an error: ActiveRecord::AdapterNotSpecified: 'production' database is not configured. Available: ["development", "test"]
When I change the database.yml
to development
and test
it works, but the outputted users are from another database used in another local Rails application. So it seems an old connection is still active? How can I make sure the right database is connected?
update:
This is now my updated database.yml
and code (see below), but it still connects to the wrong database. When I remove the development
and test
parts, it returns: ActiveRecord::AdapterNotSpecified: 'development' database is not configured. Available: ["development_prod", "development_backup"]
, so it seems it is reading the right database.yml
.
development:
adapter: postgresql
encoding: unicode
database: test_eagle_development
host: localhost
pool: 296
username: postgres
password: password1
template: template0
test:
adapter: postgresql
encoding: unicode
database: test_eagle_test
host: localhost
pool: 5
username: postgres
password: password1
template: template0
development_prod:
adapter: postgresql
encoding: unicode
database: test_eagle_development
host: localhost
pool: 296
username: postgres
password: password1
template: template0
development_backup:
adapter: postgresql
encoding: unicode
database: test_eagle_test
host: localhost
pool: 5
username: postgres
password: password1
template: template0
And code:
class Production < ActiveRecord::Base
ActiveRecord::Base.establish_connection("#{Rails.env}_prod".to_sym)
self.abstract_class = true
attr_accessor
end
class Backup < ActiveRecord::Base
ActiveRecord::Base.establish_connection("#{Rails.env}_backup".to_sym)
self.abstract_class = true
attr_accessor
end
Upvotes: 0
Views: 1967
Reputation: 7054
It must be establish_connection(:production)
instead of ActiveRecord::Base.establish_connection(:production)
The :establish_connection
method of each specific class must be called, not the parent one which is ActiveRecord::Base
. Configuring the parent class does not make sense )
Upvotes: 5
Reputation: 155
Seems like you're running rails app in development
mode.
By default rails trying to load database config and fetch connection details by RAILS_ENV
value.
I'd suggest to name connections to both match environment:
development:
adapter: postgresql
encoding: unicode
database: test_eagle_development
host: localhost
pool: 296
username: postgres
password: password1
template: template0
development_backup:
adapter: postgresql
encoding: unicode
database: test_eagle_test
host: localhost
pool: 5
username: postgres
password: password1
template: template0
class Production < ActiveRecord::Base
ActiveRecord::Base.establish_connection(Rails.env)
self.abstract_class = true
end
class Backup < ActiveRecord::Base
ActiveRecord::Base.establish_connection("#{Rails.env}_backup")
self.abstract_class = true
end
Upvotes: 0