简体   繁体   English

如何使Rails模型根据环境类型有条件地建立连接

[英]How to have a rails model establish connection conditionally based on environment type

I have a rails model that looks like this: 我有一个看起来像这样的Rails模型:

class Db_Prod_Write < ActiveRecord::Base
    establish_connection :prod_write 
    self.abstract_class = true
end

:prod_write is a production environment defined in my database.yaml file. :prod_write是我的database.yaml文件中定义的生产环境。

This model (Db_Prod_Write) is being used in another model in a method that establishes a connection. 此模型(Db_Prod_Write)正在建立连接的方法中的另一个模型中使用。 The problem is, that when the method is running in QA environment, it still runs the method in production. 问题是,当该方法在QA环境中运行时,它仍在生产环境中运行该方法。 Is there a way for me to make establish_connection :prod_write in the above code conditional so that it only establishes if the environment is production? 有没有办法让上述代码中的establish_connection :prod_write为条件,以便它仅确定环境是否为生产环境?

Thank you in advance. 先感谢您。

establish_connection(:prod_write) if Rails.env.production?

测试以下任何一项:

  • ENV['RAILS_ENV']
  • RAILS_ENV
  • Rails.env
  • Rails.env.production?

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

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