跳至内容 跳至搜索

Active Record 数据库 URL 配置

当从 URL 创建数据库配置条目时,会为每个条目创建一个 UrlConfig 对象。这可以是一个 URL 字符串,也可以是一个用 URL 替换配置哈希的哈希。

一个 URL 配置

postgres://localhost/foo

变为

#<ActiveRecord::DatabaseConfigurations::UrlConfig:0x00007fdc3238f340
  @env_name="default_env", @name="primary",
  @config={adapter: "postgresql", database: "foo", host: "localhost"},
  @url="postgres:///foo">

有关更多信息,请参阅 ActiveRecord::DatabaseConfigurations

方法
N

Attributes

[R] URL (url)

类公共方法

new(env_name, name, url, configuration_hash = {})

初始化一个新的 UrlConfig 对象

选项

  • :env_name - Rails 环境,即“development”。

  • :name - 数据库配置名称。在标准的双层数据库配置中,它默认为“primary”。在多数据库三层数据库配置中,它对应于第二层中使用的名称,例如“primary_readonly”。

  • :url - 数据库 URL。

  • :config - 配置哈希。这是包含数据库适配器、名称和其他数据库连接重要信息的哈希。

# File activerecord/lib/active_record/database_configurations/url_config.rb, line 40
def initialize(env_name, name, url, configuration_hash = {})
  super(env_name, name, configuration_hash)

  @url = url
  @configuration_hash = @configuration_hash.merge(build_url_hash)

  if @configuration_hash[:schema_dump] == "false"
    @configuration_hash[:schema_dump] = false
  end

  query_cache = parse_query_cache
  @configuration_hash[:query_cache] = query_cache unless query_cache.nil?

  to_boolean!(@configuration_hash, :replica)
  to_boolean!(@configuration_hash, :database_tasks)

  @configuration_hash.freeze
end