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

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

概述我把我的批处理文件放在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 我把我的批处理文件放在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

当执行批处理时

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功能.

总结

以上是内存溢出为你收集整理的ruby-on-rails – 如何为脚本或批处理文件设置rails_env全部内容,希望文章能够帮你解决ruby-on-rails – 如何为脚本或批处理文件设置rails_env所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1291954.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存