I want to run a rake task that can take in a specific AR id or an array of them.
It's simple using the old structure.
task :task1 => :environment => do
ids = ENV['IDS'].split(',')
p ids
end
Using the above, you can do
rake task1 IDS=1,2,3
or
rake task1 IDS=1
However, if we pass the parameters
task :task2, [:batch_ids] => :environment => do |t, args|
ids = args.batch_ids.split(',')
p ids
end
you can never use "commas" as delimiters. Only the first part (before the comma) works.
rake task2[1,2,3]
or
rake task2[1]
both default to just ids=[1]
There is no way to make this work with comma as delimiter as rake treats ',' as args delimiter and discards those that have not been declared in the task definition as it converts parameters to openstruct.
I can make this work by using some other character for delimiter (currently ;).
Any clues?