Standalone ruby — How to load different environments from database.yml

前端 未结 2 934
眼角桃花
眼角桃花 2021-02-08 10:24

I have a background process that modifies records in a database. The models connect to the database using something like this:

dbconfig = YAML::load(File.open(\'         


        
2条回答
  •  独厮守ぢ
    2021-02-08 10:46

    I would set up the connection once at the start of your background process. Once you've established the connection once, all of the models will work correctly anyway.

    Your connection establishment code would look something like this:

    @environment = ENV['RACK_ENV'] || 'development'
    @dbconfig = YAML.load(File.read('config/database.yml'))
    ActiveRecord::Base.establish_connection @dbconfig[@environment]
    

提交回复
热议问题