how to check the database name that ActiveRecord uses

Go To


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



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

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


In Rails 4.2, you can do this:


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

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


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