# How to expire fragment cache from model?

**URL:** <https://www.ruby-forum.com/t/how-to-expire-fragment-cache-from-model/75169>\
**Category:** Rails\
**Created:** [November 7, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/how-to-expire-fragment-cache-from-model/75169 "2006-11-07T23:02:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [November 7, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/how-to-expire-fragment-cache-from-model/75169/1 "2006-11-07T23:02:32Z")

</div>

Hi. I need to figure out how to call expire\_fragment from within a  
model. Seems to be a method which is not normally available to the  
model…?

Situation is that I am feeding emails in via a MailReceiver model:

```
class MyMailReceiver < ActionMailer::Base
etc ...

```

I’m using an external script to call something like “script/runner  
MyMailReceiver.receive(somedata)”, and as a result of this call I need  
to then expire some page fragment cache items

Any suggestions on how to make this happen? Should perhaps I not be  
pushing my email directly into the Model and instead via some  
controller…? Any other suggestions for how to accept external email  
into Rails?

Cheers

Ed W

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [November 8, 2006, 9:32am UTC](https://www.ruby-forum.com/t/how-to-expire-fragment-cache-from-model/75169/2 "2006-11-08T09:32:31Z")

</div>

Ed Wg wrote:

> Hi. I need to figure out how to call expire\_fragment from within a  
> model. Seems to be a method which is not normally available to the  
> model…?
> 
> Situation is that I am feeding emails in via a MailReceiver model:
> 
> ```
> class MyMailReceiver < ActionMailer::Base
> etc ...
> 
> ```
> 
> I’m using an external script to call something like “script/runner  
> MyMailReceiver.receive(somedata)”, and as a result of this call I need  
> to then expire some page fragment cache items
> 
> Any suggestions on how to make this happen? Should perhaps I not be  
> pushing my email directly into the Model and instead via some  
> controller…? Any other suggestions for how to accept external email  
> into Rails?
> 
> Cheers
> 
> Ed W

try:

include ActionController::Caching::Fragments

Long shot, but who knows. It’s probably better to include this in your  
script than the model itself, since the model itself should not know  
about caching.

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [November 8, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/how-to-expire-fragment-cache-from-model/75169/3 "2006-11-08T12:44:44Z")

</div>

> try:
> 
> include ActionController::Caching::Fragments
> 
> Long shot, but who knows. It’s probably better to include this in your  
> script than the model itself, since the model itself should not know  
> about caching.

Seems like the better thing is for the script to call a controller  
rather than a model directly?

The example on the rails wiki talks directly to the model, but I think  
this feels like a bit of a hack really…

Appreciate any thoughts…

Cheers

Ed W
