简体   繁体   English

从数据库加载rails app config

[英]Load rails app config from database

I'd like to load some config settings from the database when my Rails (3.2.13) app starts: 我的Rails(3.2.13)应用程序启动时,我想从数据库加载一些配置设置:

class MyApp < Rails::Application
  #...normal config here...

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method       = :smtp
  config.action_mailer.smtp_settings         = {
    :address              => SystemSetting.system_smtp_host,
    :port                 => SystemSetting.system_smtp_port,
    :user_name            => SystemSetting.system_smtp_username,
    :password             => SystemSetting.system_smtp_password,
    :authentication       => SystemSetting.system_smtp_authentication,
    :enable_starttls_auto => SystemSetting.system_smtp_enable_starttls_auto }
end

But it appears that the database config has not been read at this point. 但似乎此时尚未读取数据库配置。 I get an error: 我收到一个错误:

ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)

How can I accomplish this? 我怎么能做到这一点? Either make Rails initialize the database config first, or ... something else? 要么让Rails首先初始化数据库配置,要么......其他什么?

Regarding practices: Doing stuff like this can be problematic because your application relies on your database to be full when in fact it should support an empty database. 关于实践:做这样的事情可能会有问题,因为你的应用程序依赖于你的数据库是完整的,而实际上它应该支持一个空的数据库。 Two examples that I can think of are when you try to create a database from scratch (eg rake db:setup), or in tests environments. 我能想到的两个例子是当您尝试从头开始创建数据库(例如rake db:setup)或测试环境时。

However, if have no other choice: I would move the mailer initialization to your system_setting.rb or an initializer (where you are guaranteed to have a connection). 但是,如果没有其他选择:我会将邮件程序初始化移动到您的system_setting.rb或初始化程序(保证您有连接)。

config/initializers/load_system_settings.rb 配置/初始化/ load_system_settings.rb

MyApp::Application.config.action_mailer.raise_delivery_errors = true
MyApp::Application.config.action_mailer.delivery_method       = :smtp
MyApp::Application.config.action_mailer.smtp_settings         = {
  :address              => SystemSetting.system_smtp_host,
  :port                 => SystemSetting.system_smtp_port,
  :user_name            => SystemSetting.system_smtp_username,
  :password             => SystemSetting.system_smtp_password,
  :authentication       => SystemSetting.system_smtp_authentication,
  :enable_starttls_auto => SystemSetting.system_smtp_enable_starttls_auto
}

Manually connect and load values from database. 手动连接并加载数据库中的值。

class Application < Rails::Application

  # Set your app path
  app_base = 'MY_APPLICATION_PATH' 

  # Load db configs
  db_yml = YAML.load_file("#{app_base}/config/database.yml")["production"]

  # Establish db connection
  ActiveRecord::Base.establish_connection(:adapter=>db_yml['adapter'], :database=>db_yml['database'], :username=>db_yml['username'], :password=>db_yml['password']) 

  # load model if not 
  require "#{app_base}/app/model/system_setting.rb" unless defined?('SystemSetting')

  #...normal config here...

  config.action_mailer.raise_delivery_errors           = true
  config.action_mailer.delivery_method                 = :smtp
  config.action_mailer.smtp_settings                   = {
    :address                                          => SystemSetting.system_smtp_host,
    :port                                             => SystemSetting.system_smtp_port,
    :user_name                                        => SystemSetting.system_smtp_username,
    :password                                         => SystemSetting.system_smtp_password,
    :authentication                                   => SystemSetting.system_smtp_authentication,
    :enable_starttls_auto                             => SystemSetting.system_smtp_enable_starttls_auto }

  #close sql connection after loaded 
   ActiveRecord::Base.connection.close
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM