# Calling Rake Tasks From Within Ruby Code

**URL:** <https://www.ruby-forum.com/t/calling-rake-tasks-from-within-ruby-code/70411>\
**Category:** Rails\
**Created:** [September 29, 2006, 12:18am UTC](https://www.ruby-forum.com/t/calling-rake-tasks-from-within-ruby-code/70411 "2006-09-29T00:18:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 29, 2006, 12:18am UTC](https://www.ruby-forum.com/t/calling-rake-tasks-from-within-ruby-code/70411/1 "2006-09-29T00:18:25Z")

</div>

I need to call a rake task from within ruby code. I know that I can  
just do something like this:

%x[rake my\_rake\_task]

But, I need to wrap this in an ActiveRecord transaction. Is there a way  
to call a rake task w/o spawning a new process and thus gain the benefit  
of wrapping it in an ActiveRecord transaction?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 29, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/calling-rake-tasks-from-within-ruby-code/70411/2 "2006-09-29T18:27:15Z")

</div>

Sorry to ‘resurface’ this post, but I’d really like to know if anyone  
has any ideas on this one?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 5, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/calling-rake-tasks-from-within-ruby-code/70411/3 "2006-12-05T18:40:20Z")

</div>

Caleb wrote:

> I need to call a rake task from within ruby code. I know that I can  
> just do something like this:
> 
> %x[rake my\_rake\_task]
> 
> But, I need to wrap this in an ActiveRecord transaction. Is there a way  
> to call a rake task w/o spawning a new process and thus gain the benefit  
> of wrapping it in an ActiveRecord transaction?

See [Calling a task in rant at any time (not just as a prerequisi - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/75063#113616)
