I have a rake task that needs to insert a value into multiple databases.
I'd like to pass this value into the rake task from the command line, or from another rake task.
How can I do this?
I have a rake task that needs to insert a value into multiple databases.
I'd like to pass this value into the rake task from the command line, or from another rake task.
How can I do this?
While passing parameters, it is better option is an input file, can this be a excel a json or whatever you need and from there read the data structure and variables you need from that including the variable name as is the need. To read a file can have the following structure.
Example json
Execution
The ways to pass argument are correct in above answer. However to run rake task with arguments, there is a small technicality involved in newer version of rails
It will work with rake "namespace:taskname['argument1']"
Note the Inverted quotes in running the task from command line.
If you can't be bothered to remember what argument position is for what and you want do something like a ruby argument hash. You can use one argument to pass in a string and then regex that string into an options hash.
And on the command line you get.
Another commonly used option is to pass environment variables. In your code you read them via
ENV['VAR']
, and can pass them right before therake
command, likeTo pass arguments to the default task, you can do something like this. For example, say "version" is your argument:
Then you can call it like so:
or
or
However, I have not found a way to avoid specifying the task name (default or build) while passing in arguments. Would love to hear if anyone knows of a way.
Most of the methods described above did not work for me, maybe they are deprecated in the newer versions. The up-to-date guide can be found here: http://guides.rubyonrails.org/command_line.html#custom-rake-tasks
a copy-and-paste ans from the guide is here:
Invoke it like this