How do I run rake tasks within my rails application

后端 未结 5 910
青春惊慌失措
青春惊慌失措 2020-12-24 08:47

What I want to do:

In a model.rb, in after_commit, I want to run rake task ts:reindex

ts:reindex is normally run with a rake ts:index

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 09:46

    I had this same issue and couldn't get the accepted answer to work in my controller with a Rails 4 project due to a load file error. This post gave me a working solution:

    def restart_search
       require 'rake'
       spec = Gem::Specification.find_by_name 'thinking-sphinx'
       load "#{spec.gem_dir}/lib/thinking_sphinx/tasks.rb"
       Rake::Task["ts:stop"].execute
       Rake::Task["ts:start"].execute
       respond_to do |format|
         format.js { head :ok }
       end
    end
    

提交回复
热议问题