2010-02-08 37 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