# Calling Rake from INSIDE the application

**URL:** <https://www.ruby-forum.com/t/calling-rake-from-inside-the-application/154606>\
**Category:** Rails\
**Created:** [December 2, 2008, 3:53am UTC](https://www.ruby-forum.com/t/calling-rake-from-inside-the-application/154606 "2008-12-02T03:53:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dante\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f475e1/32.png) [@Dante\_R](https://www.ruby-forum.com/u/Dante_R)\
**Post date:** [December 2, 2008, 3:53am UTC](https://www.ruby-forum.com/t/calling-rake-from-inside-the-application/154606/1 "2008-12-02T03:53:37Z")

</div>

Hey there  
I would like to know if I can call a rake task from inside my Rails App.  
Something like

class MyModel \< ActiveRecord::Base  
after\_save :run\_task

def run\_task  
Rake.run(“my:little:task”)  
end  
end

This is the deal: I use thinking-sphinx and need to update a bunch of  
data  
through Model.update\_all. But I would like sphinx to index it just after  
I’m  
done, by running rake ts:index.

Is that possible?

Thanks,  
Dante

---

<div class="post-metadata">

**Author:** ![Dante\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f475e1/32.png) [@Dante\_R](https://www.ruby-forum.com/u/Dante_R)\
**Post date:** [December 2, 2008, 8:44am UTC](https://www.ruby-forum.com/t/calling-rake-from-inside-the-application/154606/2 "2008-12-02T08:44:25Z")

</div>

def run\_task  
result = %x(rake #{params[:rake\_task]})  
end

If you don’t want to hold the thread you can install the crontab gem  
and install a new crontab with a ts:in or use background-fu and push  
the job to the background…
