Reputation: 742
This is my first experience using rake tasks, but my problem is: I have a list containing ID's and I want to pass as argument to my rake task. But when I call:
url_ids.collect{ |i| i.slice! 'id=' }
system "rake scan_multiple_urls URL_IDS=#{url_ids} &"
Rails says:
[298486374,
rake aborted!
Don't know how to build task '980190962]' (see --tasks)
My rake task is this:
url_ids = ENV['URL_IDS']
puts url_ids
Upvotes: 0
Views: 109
Reputation: 665
If you want to write your own rake task you have 2 ways to do it
rails g task scan_multiple_urls my_task1 create file in lib/tasks/scan_multiple_urls.rake
it will genereate an file scan_multiple_urls.rake inside lib folder
if you open lib/scan_multiple_urls.rake
namespace :scan_multiple_urls do
desc "TODO"
task my_task1: :environment do
// write your code here
end
end
for example I have list of user ids I need to update all user country to india
namespace :my_namespace do
desc "Update user details Eg: rake my_namespace:my_task1 USER_IDS='1,3,4'"
task my_task1: :environment do
user_ids = ENV['USER_IDS'] || ENV['user_ids']
user_ids = user_ids.split(",") if user_ids.present?
User.where(id: user_ids).update_all(country: "india")
p "Update successfull"
end
end
when running the rake my_namespace:my_task1 USER_IDS='1,3,4' all the users country get updated to india
also you can run this by rails console
arr = url_ids.collect{ |i| i.slice! 'id=' } str = arr.join(',')
system "rake my_namespace:my_task1 USER_IDS=#{str} &"
Upvotes: 1
Reputation: 121
arr = url_ids.collect{ |i| i.slice! 'id=' }
str = arr.join(',')
system "rake scan_multiple_urls URL_IDS=#{str} &"
url_ids = ENV['URL_IDS'].split(',')
puts url_ids
Join it as a string and while fetching split it back.
Upvotes: 2