How to pass command line arguments to a rake task

后端 未结 19 2132
走了就别回头了
走了就别回头了 2020-11-22 10:13

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

19条回答
  •  长情又很酷
    2020-11-22 10:50

    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.

      namespace :name_sapace_task do
        desc "Description task...."
          task :name_task  => :environment do
            data =  ActiveSupport::JSON.decode(File.read(Rails.root+"public/file.json")) if defined?(data)
        # and work whit yoour data, example is data["user_id"]
    
        end
      end
    

    Example json

    {
      "name_task": "I'm a task",
      "user_id": 389,
      "users_assigned": [389,672,524],
      "task_id": 3
    }
    

    Execution

    rake :name_task 
    

提交回复
热议问题