Ruby-on-rails 在 Rails 中显示主机名和数据库名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/267219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:46:36  来源:igfitidea点击:

Displaying host and database names in Rails

ruby-on-rails

提问by hectorsq

I want to display the current host and database names in a view.

我想在视图中显示当前的主机名和数据库名。

Where can I get these names?

我在哪里可以得到这些名字?

Are there some predefined environment or global variables?

是否有一些预定义的环境或全局变量?

采纳答案by Robert Gamble

You can create a rails configuration object and obtain the necessary information from it:

您可以创建一个 rails 配置对象并从中获取必要的信息:

config   = Rails::Configuration.new
host     = config.database_configuration[RAILS_ENV]["host"]
database = config.database_configuration[RAILS_ENV]["database"]

See the documentationfor Rails::Configuration for details.

有关详细信息,请参阅Rails::Configuration的文档

回答by Fernando Almeida

With Rails 3 you can use

使用 Rails 3,您可以使用

Rails.configuration.database_configuration[Rails.env]

or

或者

Rails.application.config.database_configuration[Rails.env]

or

或者

ActiveRecord::Base.connection_config

回答by craic.com

The Rails::configuration.new answer does not work in Rails 3

Rails::configuration.new 答案在 Rails 3 中不起作用

config   = Rails::Configuration.new
NoMethodError: undefined method `new' for Rails::Configuration:Module
[...]

If you are using MySQL or Postgres then this works

如果您使用的是 MySQL 或 Postgres,那么这有效

ActiveRecord::Base.connection.current_database

But this only works with drivers that have implemented that method. For example, it will not work for SQLite.

但这仅适用于已实现该方法的驱动程序。例如,它不适用于 SQLite。

回答by Ryan Long

On Rails 3, this is most easily accessed via Rails::Application.config, like so:

在 Rails 3 上,这最容易通过 访问Rails::Application.config,如下所示:

    YourApplicationClassName::Application.config.database_configuration[::Rails.env]

If you try to call it directly on Railsinstead of YourApplicationClassName, rails will give you a deperecation warning. If you don't know what your application constant is, try Rails.application.class.namein a Rails console for your app.

如果你尝试直接调用它Rails而不是YourApplicationClassName,rails 会给你一个 deperecation 警告。如果您不知道您的应用程序常量是什么,请Rails.application.class.name在您的应用程序的 Rails 控制台中尝试。

回答by joshmckin

The method below is nice when you are connected to multiple databases. Tested on Sqlite3 and Mysql2

当您连接到多个数据库时,下面的方法很好。在 Sqlite3 和 Mysql2 上测试

MyModel.connection.instance_variable_get(:@config)[:database]

回答by mikowiec

Tested in Rails console:

在 Rails 控制台中测试:

ActiveRecord::Base.connection.instance_variable_get(:@config)

next info should be out: adapter, host, encoding, reconnect, database name, username, pass

下一个信息应该是:适配器、主机、编码、重新连接、数据库名称、用户名、通过

回答by toomaj

Just this should work ActiveRecord::Base.connection.database_name

这应该工作 ActiveRecord::Base.connection.database_name