从数据库加载rails app config

时间:2014-02-20 04:44:21

标签: ruby-on-rails ruby-on-rails-3 activerecord configuration

当我的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

但似乎此时尚未读取数据库配置。我收到一个错误:

ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)

我怎样才能做到这一点?要么让Rails首先初始化数据库配置,要么......其他什么?

2 个答案:

答案 0 :(得分:2)

关于实践:做这样的事情可能会有问题,因为您的应用程序依赖于您的数据库已满,而实际上它应该支持空数据库。我可以想到的两个例子是当您尝试从头开始创建数据库(例如rake db:setup)或测试环境时。

但是,如果没有其他选择:我会将邮件程序初始化移动到您的system_setting.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
}

答案 1 :(得分:0)

手动连接并加载数据库中的值。

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