![](/img/trans.png)
[英]how to run this rake task with 2 args and environment in rails 4?
[英]Force Rake Task To Run in Specific Rails Environment
我需要從另一個Rake任務運行一系列Rake任務。 前三個任務需要在開發環境中運行,但最終任務需要在登台環境中運行。 該任務依賴於:environment
,導致在任務運行之前加載Rails開發環境。
但是,我需要在暫存環境中執行最終任務。
在調用rake任務之前傳遞RAILS_ENV=staging
標志是不好的,因為此時已經加載了環境,所有這一切都將設置標志,而不是加載登台環境。
有沒有辦法可以在特定環境中強制執行rake任務?
我之前已經完成了這種方式,盡管不是最優雅的方式:
task :prepare do
system("bundle exec rake ... RAILS_ENV=development")
system("bundle exec rake ... RAILS_ENV=development")
system("bundle exec rake ... RAILS_ENV=test")
system("bundle exec rake ... RAILS_ENV=test")
system("bundle exec rake ... RAILS_ENV=test")
system("bundle exec rake ... RAILS_ENV=test")
end
它總是對我有用。 我很想知道是否有更好的方法。
我解決它的方法是在調用任務之前添加一個依賴項來設置rails env:
namespace :foo do
desc "Our custom rake task"
task :bar => ["db:test:set_test_env", :environment] do
puts "Custom rake task"
# Do whatever here...
puts Rails.env
end
end
namespace :db do
namespace :test do
desc "Custom dependency to set test environment"
task :set_test_env do # Note that we don't load the :environment task dependency
Rails.env = "test"
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.