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?
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"]
ActiveRecord::Base.connection.execute("USE bleh")
This will: ActiveRecord::Base.connection.current_database
Gerry 2017-10-22 03:20
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
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.
Rails.configuration.database_configuration[Rails.env]
MacKinley Smith 2014-06-07 18:54