我正在尝试在单个任务中执行多个rake任务。 所有rake任务都用于更新各个表的列。 例如 如果health_post_id> 100000 删除该记录
现在我需要通过命令行传递100000作为参数。但我无法弄明白 这是代码
if Rails.env.development? or Rails.env.test?
namespace :clear_data do
desc 'clear time slots'
task :clear_time_slots => :environment do
TimeSlot.where('health_post_id > ?', p).each do |time_slots|
time_slots.destroy
end
end
desc "Clean the Practices table"
task :clear_practice_records => :environment do
Practice.where('health_post_id > ?', p).each do |practices|
practices.destroy
end
end
desc "clean database"
task :clear_database => :environment do |p|
Rake::Task['clear_data:clear_practice_records'].execute
Rake::Task['clear_data:clear_time_slots'].execute
end
end
end
答案 0 :(得分:1)
我不太确定,但我想你应该可以做到:
if Rails.env.development? or Rails.env.test?
namespace :clear_data do
desc 'clear time slots'
task :clear_time_slots, [:post_id] => :environment do |_, args|
TimeSlot.where('health_post_id > ?', args[:post_id]).destroy_all
end
desc "Clean the Practices table"
task :clear_practice_records, [:post_id] => :environment do |_, args|
Practice.where('health_post_id > ?', args[:post_id]).destroy_all
end
desc "clean database"
task :clear_database, [:post_id] => :environment do |_, args|
max = args[:post_id]
Rake::Task['clear_data:clear_practice_records'].execute(post_id: max)
Rake::Task['clear_data:clear_time_slots'].execute(post_id: max)
#OR
#Rake::Task['clear_data:clear_practice_records'].invoke(max)
#Rake::Task['clear_data:clear_time_slots'].invoke(max)
end
end
end