# Problem with method that performs a rake

**URL:** <https://www.ruby-forum.com/t/problem-with-method-that-performs-a-rake/173587>\
**Category:** Rails\
**Created:** [August 11, 2009, 7:33pm UTC](https://www.ruby-forum.com/t/problem-with-method-that-performs-a-rake/173587 "2009-08-11T19:33:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [August 11, 2009, 7:33pm UTC](https://www.ruby-forum.com/t/problem-with-method-that-performs-a-rake/173587/1 "2009-08-11T19:33:27Z")

</div>

hello,

I am new to programming and am trying to create a clear\_cache method.  
This method is invoked when a submit button is pressed on a partial.

def clear\_cache  
Dir.chdir(‘…/…/…/…/…/’) #This moves up to the appropriate  
directory  
system(‘rake _0.7.3_ tmp:cache:clear’)  
redirect\_to ‘[http://127.0.0.1:3000/admin](http://127.0.0.1:3000/admin)’  
end

The method is certainly being invoked as the redirect is working fine,  
however, the method skips over the first two lines, no error messages  
are appearing but the cache is not being cleared. if this method is  
invoked in a seperate file in the same directory, using a call to the  
method and the f5 button, the cache is clearing.

def clear\_cache  
Dir.chdir(‘…/…/…/…/…/’)  
system(‘rake _0.7.3_ tmp:cache:clear’)  
redirect\_to ‘[http://127.0.0.1:3000/admin](http://127.0.0.1:3000/admin)’  
end

clear\_cache

Am pretty confused as to why in our controller the first two lines are  
being skipped over and would greatly appreciate any help i recieve with  
this issue.

regards,  
mike

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [August 12, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/problem-with-method-that-performs-a-rake/173587/2 "2009-08-12T19:21:29Z")

</div>

The chdir line is almost certainly wrong - going to RAILS\_ROOT would  
be much more reliable.

I’m also unsure what a novice programmer is doing shelling out to a  
2.5 year old version of Rake. Is there some reason why you’re stuck on  
0.7.3?

Hardcoding the path to the redirect is also incorrect - you’ll  
probably want something more like “redirect\_to ‘/admin’”, which will  
work even if the app isn’t running on localhost…

Finally, the entire premise here seems a little off - you haven’t  
mentioned what’s in the cache dir that you want to clear out, but  
there are much better ways of handling it in most circumstances. Take  
a look at the framework docs for ActionController::Caching  
(specifically, sweepers and friends).

–Matt J.

On Aug 11, 1:33 pm, Michael W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [August 11, 2009, 8:23pm UTC](https://www.ruby-forum.com/t/problem-with-method-that-performs-a-rake/173587/3 "2009-08-11T20:23:53Z")

</div>

Michael W. wrote:

> def clear\_cache  
> Dir.chdir(‘…/…/…/…/…/’)  
> system(‘rake _0.7.3_ tmp:cache:clear’)  
> redirect\_to ‘[http://127.0.0.1:3000/admin](http://127.0.0.1:3000/admin)’  
> end
> 
> clear\_cache

Mike, they are probably not being skipped but your system call is  
erroring out due to not being in the place you think you are…

The relative addressing you are using above looks rather fragile… any  
way to use the context path?

use the log to output the Dir.pwd … or make sure rake is in your path…

lastly, I believe you can run rake from within ruby which is probably a  
better alternative than to use the system…

ilan

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [August 12, 2009, 8:00pm UTC](https://www.ruby-forum.com/t/problem-with-method-that-performs-a-rake/173587/4 "2009-08-12T20:00:52Z")

</div>

Michael W. wrote:

> hello,
> 
> I am new to programming and am trying to create a clear\_cache method.  
> This method is invoked when a submit button is pressed on a partial.

You _might_ want to do this via a method call to the model after the  
update/save is completed.

> def clear\_cache  
> Dir.chdir(‘…/…/…/…/…/’)  
> system(‘rake _0.7.3_ tmp:cache:clear’)  
> redirect\_to ‘[http://127.0.0.1:3000/admin](http://127.0.0.1:3000/admin)’  
> end

tmp:cache:clear is an awfully heavy-handed way to clear the cache. If  
you smart-code your cache keys, a simple call to Rails.cache.delete from  
within the model selectively dump cache fragments, whether that  
‘fragment’ is a little bit of HTML or a whole page, just depends on your  
cache key.

Your controllers can use Rails.cache.exist?(fragment\_name) to avoid the  
DB hit:

Your views can contain code like:  
\<% cache(fragment\_name) do %\>  
regular erb or haml or whatever  
\<% end %\>

and the models expire their own fragments via code using  
Rails.cache.delete(fragment\_name)
