LoginSignup
2
0

More than 5 years have passed since last update.

Rakeタスクの中でRakeタスクをループするtips

Posted at

Rakeタスクの中でループして他のRakeタスクを呼びたくなっちゃう時ありますよね。
その時にはRake::Task#reenableを使います。


  task :task_something do |t, args|
    (1..10).each_with_index do |num, i|
      if i == 0
        Rake::Task["batch:task_something_2"].invoke(num)
      else
        Rake::Task["batch:task_something_2"].reenable
        Rake::Task["batch:task_something_2"].invoke(num)
      end
    end
  end

ここまで書いて面倒くさいなと調べたらexecuteでいけました


  task :task_something do |t, args|
    (1..10).each_with_index do |num, i|
      if i == 0
        Rake::Task["batch:task_something_2"].execute(number: num)
      end
    end
  end

ただし、batch:task_something_2でActiveRecordのexecuteメソッドを使ってるとこのままでは動きません。


  task task_something: :environment do |t, args|
    (1..10).each_with_index do |num, i|
      if i == 0
        Rake::Task["batch:task_something_2"].execute(number: num)
      end
    end
  end

environmentが無いとDB接続出来ないですね・・ハマりましたorz

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0