# Expire\_action in model possible

**URL:** <https://www.ruby-forum.com/t/expire-action-in-model-possible/121857>\
**Category:** Rails\
**Created:** [December 2, 2007, 1:04am UTC](https://www.ruby-forum.com/t/expire-action-in-model-possible/121857 "2007-12-02T01:04:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [December 2, 2007, 1:04am UTC](https://www.ruby-forum.com/t/expire-action-in-model-possible/121857/1 "2007-12-02T01:04:08Z")

</div>

Hi all,

Is it possible to use expire\_action in a model?  
I tried:  
after\_save :clean\_balance\_cache

clean\_balance\_cache holds the expire\_action method. But then I get an  
error:  
undefined method `expire\_action’ for #Declaration:0xb69d19b0

Is there a way to make this work? Or should I make it work another way?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [December 2, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/expire-action-in-model-possible/121857/2 "2007-12-02T12:40:43Z")

</div>

I don’t know if it’s possible (you might be able to use ‘include  
ActionController::Caching::Actions’ in the model) but I prefer my  
cache expiration to be outside of the model and in sweeper files…

class DeclarationSweeper \< ActionController::Caching::Sweeper

observe Declaration

def after\_create(page)  
# expire something…  
end

end

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [December 2, 2007, 7:27pm UTC](https://www.ruby-forum.com/t/expire-action-in-model-possible/121857/3 "2007-12-02T19:27:27Z")

</div>

idleFingers wrote:

> I don’t know if it’s possible (you might be able to use ‘include  
> ActionController::Caching::Actions’ in the model) but I prefer my  
> cache expiration to be outside of the model and in sweeper files…
> 
> class DeclarationSweeper \< ActionController::Caching::Sweeper
> 
> observe Declaration
> 
> def after\_create(page)  
> # expire something…  
> end
> 
> end

Thanks. But the cache I want to sweep is based on the data of two  
different models. Is it possible to call the same method  
(clean\_balance\_cache) in two different sweeper files? The method  
consists of `expire\_action’ and some other things.

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [December 2, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/expire-action-in-model-possible/121857/4 "2007-12-02T20:06:05Z")

</div>

Leon B. wrote:

> idleFingers wrote:
> 
> > I don’t know if it’s possible (you might be able to use ‘include  
> > ActionController::Caching::Actions’ in the model) but I prefer my  
> > cache expiration to be outside of the model and in sweeper files…
> > 
> > class DeclarationSweeper \< ActionController::Caching::Sweeper
> > 
> > observe Declaration
> > 
> > def after\_create(page)  
> > # expire something…  
> > end
> > 
> > end
> 
> Thanks. But the cache I want to sweep is based on the data of two  
> different models. Is it possible to call the same method  
> (clean\_balance\_cache) in two different sweeper files? The method  
> consists of `expire\_action’ and some other things.

This worked:

class BalanceSweeper \< ActionController::Caching::Sweeper

observe Declaration, Deposit

def after\_create(data)  
clean\_balance\_cache  
end

def after\_save(data)  
clean\_balance\_cache  
end

def after\_destroy(data)  
end

def clean\_balance\_cache  
#Expire the cache for the module right  
#expire\_fragment(:controller =\> ‘application’, :action =\> ‘balans’)  
expire\_action :controller =\> ‘ajax’, :action =\> ‘module\_balans’  
end

end

Thanks!
