加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何为脚本或批处理文件设置rails_env

发布时间:2020-12-17 03:08:56 所属栏目:百科 来源:网络整理
导读:我把我的批处理文件放在lib文件夹中 并使用rails db配置,这样的active-record. require "#{File.dirname(__FILE__)}/../config/environment.rb"class Batch def hello Message.new do |t| t.title = "hello" t.save end endendbatch = Batch.newbatch.hello
我把我的批处理文件放在lib文件夹中
并使用rails db配置,这样的active-record.

require "#{File.dirname(__FILE__)}/../config/environment.rb"

class Batch
  def hello
    Message.new do |t|
      t.title = "hello"
      t.save
    end
  end
end

batch = Batch.new
batch.hello

当执行批处理时

ruby lib/batch.rb

在开发环境中没关系

但生产环境仍然保存开发数据库……

我如何设置rails_env batch.rb这样

ruby lib/batch.rb RAILS_ENV=production

解决方法

初始化Rails环境,而不是放置

require "#{File.dirname(__FILE__)}/../config/environment.rb"

使用脚本/运行器启动批处理文件,并使用-e选项指定环境

例如

script/runner -e production lib/batch.rb

我认为以上是The Rails编写和执行脚本的方式,需要初始化Rails框架才能工作.正如neutrino所说的那样,替代方案是在命令前加上RAILS_ENV = value,例如:

$RAILS_ENV=production lib/batch.rb

这是在执行命令之前设置环境变量的标准shell功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读