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
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
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:
This code automagically loads the Rake tasks for your Rails application without you even knowing how your application is named :)
Have you tried `rake ts:reindex`?
If you wish this rake code to run during the request cycle then you should avoid running rake via
system
or any of the exec family (including backticks) as this will start a new ruby interpreter and reload the rails environment each time it is called.Instead you can call the Rake commands directly as follows :-
Note: in Rails 4+, you'll use
Rails.root
instead ofRAILS_ROOT
.And then just use
SomeModel.run_rake("ts:reindex")
The key parts here are to
require rake
and make sure you load the file containing the task definitions.Most information obtained from http://railsblogger.blogspot.com/2009/03/in-queue_15.html
And then just use
SomeModel.run_rake("ts:reindex")
.