Ruby on rails 访问database.yml中的自定义类

Ruby on rails 访问database.yml中的自定义类,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我的rails应用程序中有以下类: class Global class << self def method_missing(message) ENV[message.to_s] end def SOME_ENV_VAR ENV['SOME_ENV_VAR'].present? ? ENV['SOME_ENV_VAR'].to_i * 5 : nil end end 类全局 class我发现这个问题最简单的解决方案是

我的rails应用程序中有以下类:

class Global
  class << self

    def method_missing(message)
      ENV[message.to_s]
    end

    def SOME_ENV_VAR
      ENV['SOME_ENV_VAR'].present? ? ENV['SOME_ENV_VAR'].to_i * 5 : nil
    end
end
类全局

class我发现这个问题最简单的解决方案是需要
/config/application.rb
文件中的
/app/global/global.rb

require_relative '../app/global/global'

module MyApp
  class Application < Rails::Application
    # all of the general setup
  end
end
require_relative'../app/global/global'
模块MyApp
类应用程序
我认为,如果您将这个
全局
类放入
config/initializers/
文件夹,它将在启动时加载,并且(希望)在DB连接设置之前,您可以尝试在配置之前的
回调中初始化代码,请参见,例如。,我尝试了这两种选择,虽然我认为我可以让它们一起工作,但我相信我找到了一个更简单的解决方案,我已经发布了。
require_relative '../app/global/global'

module MyApp
  class Application < Rails::Application
    # all of the general setup
  end
end