How to pass arguments into a Rake task with environment in Rails?
TLDR;
task :t, [args] => [deps]
Original Answer
When you pass in arguments to rake tasks, you can require the environment using the :needs option. For example:
desc "Testing environment and variables"
task :hello, :message, :needs => :environment do |t, args|
args.with_defaults(:message => "Thanks for logging on")
puts "Hello #{User.first.name}. #{args.message}"
end
Updated per @Peiniau's comment below
As for Rails > 3.1
task :t, arg, :needs => [deps] # deprecated
Please use
task :t, [args] => [deps]
How to pass command line arguments to a rake task
Options and dependencies need to be inside arrays:
namespace :thing do
desc "it does a thing"
task :work, [:option, :foo, :bar] do |task, args|
puts "work", args
end
task :another, [:option, :foo, :bar] do |task, args|
puts "another #{args}"
Rake::Task["thing:work"].invoke(args[:option], args[:foo], args[:bar])
# or splat the args
# Rake::Task["thing:work"].invoke(*args)
end
end
Then
rake thing:work[1,2,3]
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}
rake thing:another[1,2,3]
=> another {:option=>"1", :foo=>"2", :bar=>"3"}
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}
NOTE: variable
task
is the task object, not very helpful unless you know/care about Rake internals.
RAILS NOTE:
If running the task from Rails, it's best to preload the environment by adding
=> [:environment]
which is a way to setup dependent tasks.
task :work, [:option, :foo, :bar] => [:environment] do |task, args|
puts "work", args
end
passing argument to rake task in Ruby on Rails
I'm not so sure, but I guess you should be able to do:
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
RAILS - Passing parameters to a Rake task
[:arg1]
must be args[:arg1]
(or whatever name you use as block argument). Here's the code:
task :export, [:arg1] => :environment do |t, args|
puts "Exporting..."
Importer.export_to_csv(args[:arg1])
puts "done."
end
Usage:
rake export[foo1]
Passing arguments to a method in Rake Task
You have access to the parameter like a hash-parameter (1):
task :daily_sales_commission, [:arg1, :arg2] do |t,args|
#args can be treated like a hash.
get_date(args[:arg1], args[:arg2]) #<- call your method
end
def get_date(arg1,arg2)
#some code
end
These parameters are not common command line parameters, you must use them directly as parameters of the rake parameter. The example takes 1 and 2 as parameter values:
rake daily_sales_commission[1,2]
(1) It is not a Hash, it is a Rake::TaskArguments-object. But you can also use the []
to access the parameters.
Problem with passing argument to rake task
You want
arg[:argument].blank?
because arg
is a hash with :argument
key.
On a side note: the following would be more descriptive definition of a task (note plural args
and location
since it looks like you're passing location):
task :bar, [:location] => :environment do |_task, args|
if args[:location].blank?
# do stuff
else
# do other stuff
end
end
Related Topics
How to Understand Symbols in Ruby
Ruby Array Access 2 Consecutive(Chained) Elements At a Time
What Does the "===" Operator Do in Ruby
Difference Between Include and Require in Ruby
How to Avoid Running Activerecord Callbacks
How to Find and Return a Duplicate Value in Array
Redirect_To Using Post in Rails
Best Practices With Stdin in Ruby
How to Modify Path For Homebrew
How to Get Source Code of a Method Dynamically and Also Which File Is This Method Locate In
Ruby Operator Precedence Table
Find_Spec_For_Exe': Can't Find Gem Bundler (≫= 0.A) (Gem::Gemnotfoundexception)
How to Seed a Database in Rails
How to Work With Two Different Databases in Rails With Active Records