How to run rake tasks from console?

后端 未结 5 407
南旧
南旧 2021-01-30 00:37

I want to invoke my rake task from console. Is it doable? if yes, how to do so?

I tried this on console:

require \         


        
相关标签:
5条回答
  • 2021-01-30 01:05

    The easiest way to do it is to run %x[command] from the irb. I'm not sure if what you want to achieve though.

    %x[rake db:migrate]
    

    EDIT: I highly recommend to use .invoke as Daniel says in the accepted answer.

    0 讨论(0)
  • 2021-01-30 01:05

    The easy way is:

    Rails.application.load_tasks
    Rake::Task['my_task'].invoke
    
    0 讨论(0)
  • 2021-01-30 01:17

    Running your Rake tasks requires two steps:

    1. Loading Rake
    2. Loading your Rake tasks

    You are missing the second step.

    Normally this is done in the Rakefile, but you have to do it manually here:

    require 'rake'
    Rails.application.load_tasks # <-- MISSING LINE
    Rake::Task['my_task'].invoke
    
    0 讨论(0)
  • 2021-01-30 01:19

    Just a note that if you are in the rails console via rails c you can just call/run the rake task method by irb(main):001:0> TaskClassName.new.my_task

    0 讨论(0)
  • 2021-01-30 01:23

    I am using rails 5.x.x, and was in the need the do the same form rails console.
    I have create rake task here-

    app/lib/task_to_execute.rake
    

    Here is the command worked for me-

    Load Rails.application.load_tasks

    Rake::Task['task_to_execute:task_name'].invoke
    

    Worked for me!

    0 讨论(0)
提交回复
热议问题