how to check the database name that ActiveRecord uses

Go To StackoverFlow.com

25

In database.yml you define all the settings. How can I access those settings from ruby? I've looked in App::Application::config, but can't find it there. Also, I remember people were able to configure database settings without yaml, does anyone know how?

2012-04-03 21:21
by m33lky


39

Rails.configuration.database_configuration

This will give you a hash table with the configurations for each of your environments. E.g. to get your development database name:

Rails.configuration.database_configuration["development"]["database"]
2012-04-03 21:27
by tsherif
To get the current database configuration: Rails.configuration.database_configuration[Rails.env]MacKinley Smith 2014-06-07 18:54
When I try input your command I get this error: "Rails.configuration.database_configuration[development][database]: command not found" - any idea why - VoA 2015-12-08 21:33
If somebody adds the following anywhere in your code base, database_configuration will not give you the database being used: ActiveRecord::Base.connection.execute("USE bleh")

This will: ActiveRecord::Base.connection.current_databaseGerry 2017-10-22 03:20



39

In Rails 4.2, you can do this:

ActiveRecord::Base.connection.current_database

You can also ask specific models for their database (since it's possible to use different databases per model):

User.connection.current_database
2016-11-04 15:45
by iGEL
This is also a better answer as a simple ActiveRecord::Base.connection.execute("USE bleh") would make reading from the database config pointless - Gerry 2017-10-22 03:22


1

To piggyback on the comments from tsherif, you can run the Rails.configuration commands inside the rails console (rails c) to get the database names.

2016-08-16 00:59
by tommyb67
Then to actually run the reset you use heroku pg:reset DATABASE_URL, then follow the prompt - tommyb67 2016-08-16 01:04