在 Rails 上将参数传递给 Ruby 中的 rake 任务
passing argument to rake task in Ruby on Rails
我正在尝试在单个任务中执行多个 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
我不太确定,但我想你应该能够做到:
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
我正在尝试在单个任务中执行多个 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
我不太确定,但我想你应该能够做到:
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