2010-02-08 140 views
4

我把我的批处理文件放在lib文件夹 中,并使用rails db configuration,active-record像这样。如何设置脚本或批处理文件的rails_env

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 

时EXCUTE一批

ruby lib/batch.rb 

在开发环境中这是确定

,但生产环境还是节省开发数据库...

如何设置RAILS_ENV batch.rb这样

ruby lib/batch.rb RAILS_ENV=production 

回答

5

初始化Rails环境,而不是使用script/runner

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

启动您的批处理文件,并与-e选项

例如指定环境

script/runner -e production lib/batch.rb 

我认为以上是Rails编写和执行需要Rails框架初始化才能工作的脚本的方法。如neutrino所述,替代方案是在命令前加上RAILS_ENV = ,例如,

$ RAILS_ENV=production lib/batch.rb 

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

+0

感谢非常有用的〜 – seapy 2010-02-08 17:49:37

2

仅供参考,而不脚本/亚军:

RAILS_ENV=production ruby lib/batch.rb