# Expire\_fragment in observer/model? Works fine in Sweepers

**URL:** <https://www.ruby-forum.com/t/expire-fragment-in-observer-model-works-fine-in-sweepers/155702>\
**Category:** Rails\
**Created:** [December 15, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/expire-fragment-in-observer-model-works-fine-in-sweepers/155702 "2008-12-15T19:09:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [December 15, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/expire-fragment-in-observer-model-works-fine-in-sweepers/155702/1 "2008-12-15T19:09:22Z")

</div>

I’ve pretty much tried everything, but it seems impossible to use  
expire\_fragment from models? I know you’re not supposed to and it’s  
non-MVC, but surely there much be some way to do it.

I created a module in /lib/cache\_helper.rb with all my expire helpers,  
within each are just a bunch of expire\_fragment calls. I have all my  
cache sweepers setup under /app/sweepers and have an “include  
CacheHelper” in my application controller so expiring cache within the  
app when called via controllers works fine.

Then things is I have some external daemons and especially some  
recurring cron tasks which call a rake task that calls a certain  
method. This method does some processing and inputs entries into the  
model, after which I need to expire cache. What’s the best way to do  
this as I can’t specify “cache\_sweeper” within the model.

Straight up observers seem to be the best solution but then it  
complains about expire\_fragment being undefined etc etc, I’ve even  
tried including the ActionController caching classes into the observer  
but that didn’t work. I’d love some ideas of how to create a solution  
for this. Thanks.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [September 8, 2010, 4:30pm UTC](https://www.ruby-forum.com/t/expire-fragment-in-observer-model-works-fine-in-sweepers/155702/2 "2010-09-08T16:30:27Z")

</div>

Steven Cummings wrote:

> Had this problem too and wrote up the solution the other night on my  
> blog.
> 
> [http://stevencummings.name/2010/09/07/expire\_fragment-with-rake-and-as-a-model-only-observer](http://stevencummings.name/2010/09/07/expire_fragment-with-rake-and-as-a-model-only-observer)

Interesting solution…

Related to model-based cache expiration, in one large-ish Rails project,  
the sidebar for a ‘show’ of an entity contains a view of all the related  
entities in the application. We were getting hammered on both db access  
and rendering time, so I implemented fragment caching in a multi-tiered  
approach, and left it to the models to expire caches of their data – a  
fine line to walk, but that worked the best for us.

We used a Rails.cache.delete() call from within the model to handle the  
fragment expiration, with the new/edited/deleted model selecting the  
appropriate caches to expire according to its related models.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [September 8, 2010, 3:20pm UTC](https://www.ruby-forum.com/t/expire-fragment-in-observer-model-works-fine-in-sweepers/155702/3 "2010-09-08T15:20:44Z")

</div>

Marston A. wrote:

> I’ve pretty much tried everything, but it seems impossible to use  
> expire\_fragment from models? I know you’re not supposed to and it’s  
> non-MVC, but surely there much be some way to do it.
> 
> I created a module in /lib/cache\_helper.rb with all my expire helpers,  
> within each are just a bunch of expire\_fragment calls. I have all my  
> cache sweepers setup under /app/sweepers and have an “include  
> CacheHelper” in my application controller so expiring cache within the  
> app when called via controllers works fine.
> 
> Then things is I have some external daemons and especially some  
> recurring cron tasks which call a rake task that calls a certain  
> method. This method does some processing and inputs entries into the  
> model, after which I need to expire cache. What’s the best way to do  
> this as I can’t specify “cache\_sweeper” within the model.
> 
> Straight up observers seem to be the best solution but then it  
> complains about expire\_fragment being undefined etc etc, I’ve even  
> tried including the ActionController caching classes into the observer  
> but that didn’t work. I’d love some ideas of how to create a solution  
> for this. Thanks.

Had this problem too and wrote up the solution the other night on my  
blog.

[http://stevencummings.name/2010/09/07/expire\_fragment-with-rake-and-as-a-model-only-observer](http://stevencummings.name/2010/09/07/expire_fragment-with-rake-and-as-a-model-only-observer)
