0

我实际上很少有自定义 rake 任务。我想做的是创建一个任务,当您简单地调用它时,它将在两个环境中执行。我的意思是,当我运行这个时:

rake initialize_global_settings

我希望这在开发测试环境中执行。

实际上我受到限制这样做:

rake initialize_global_settings(这将默认在开发环境中执行,我真的不知道为什么)然后我这样做: rake initialize_global_settings RAILS_ENV=test

是否有可能同时完成一项任务?

这是我的任务:

task :initialize_global_settings => :environment do
  puts "Generating all global settings parameters..."
  parameters = ["few", "parameters", "here"]

  parameters.each do |param|
    glob_set = GlobalSetting.new(:field_name => param,
                                  :field_value => "")
    if glob_set.save
      puts "#{param} created"
    else
      puts "#{param} already exist"
    end
  end

  puts "done."
end
4

1 回答 1

1

我找到了一个解决方案:

task :initialize_global_settings => :environment do
   puts "Generating all global settings parameters..."
   parameters = ["few", "parameters", "here"]
   environments = ['development', 'test']

   environments.each do |environment|
     Rails.env = environment
     puts "\nRunning Task in "+environment+" environment \n\n"
     parameters.each do |param|
       glob_set = GlobalSetting.new(:field_name => param,
                                     :field_value => "")
       if glob_set.save
         puts "#{param} created"
       else
         puts "#{param} already exist"
       end
     end
     puts "\nParameters have been set"
    end
 end

它有效,但我在测试和开发环境中设置的相同变量之间存在冲突,我不知道为什么。

于 2013-07-23T08:05:04.053 回答