HansPetter.no :blogg

Rake/rails task with attributes

Passing arguments to rake/rails tasks[is,easy].

Rails: Create your task in /lib/tasks/<your-name-here>.task. You can view all available tasks with rails -T

desc "Rake with attributes"
task :test_with_attr, [:one, :two] => [:environment] do |task, args|
  puts args.one
  puts args.two
  args.each do |k,v|
    puts "#{k}: #{v}"
  end
end
  

Invoke with: $ rails test_with_attr[123,456]

To invoke from another rake task use:

Rake::Task["test_with_attr"].invoke(123,456)

Missing arguments return nil.

If you are using rake without rails skip :environment

task :test_with_attr, [:one, :two] do |task, args|
end