# Making 'expire\_fragment' available to a model?

**URL:** <https://www.ruby-forum.com/t/making-expire-fragment-available-to-a-model/117844>\
**Category:** Rails\
**Created:** [October 21, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/making-expire-fragment-available-to-a-model/117844 "2007-10-21T13:07:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [October 21, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/making-expire-fragment-available-to-a-model/117844/1 "2007-10-21T13:07:52Z")

</div>

I noticed a lot of repetition across different Sweeper models in my  
app expiring the same fragments and so have been trying to move all  
the expire\_fragment calls into another model which the sweepers can  
then use but it’s not working as I’d hoped…

I’ve created a CacheDestroyer model and want to be able to call  
something to the effect of: CacheDestroyer.clear\_all\_page\_relations  
but I’m struggling to find how to make the “expire\_fragment” method  
available to this model.

I’ve tried putting “include ActionController::Caching::Fragments” in  
the CacheDestroyer model but it doesn’t seem to be working (undefined  
method `expire\_fragment’ for CacheDestroyer:Class (NoMethodError))

Any ideas how I can get the “expire\_fragment” method to be available  
to this model?

Thanks in advance for any help,  
Damien

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [October 21, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/making-expire-fragment-available-to-a-model/117844/2 "2007-10-21T13:59:54Z")

</div>

The problem with trying to access what the controller did/does  
[caching and sweeping the cache] from the model is that it breaks one  
of Rails’ philosophies: MVC [the separation of model - view -  
controller code]. The proper solution is to use Sweeper classes.  
There’s a good bit of information out there on these as well. The guys  
over at RailsEnvy have a good tutorial you might want to check out:  
[http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2](http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2).  
Hope that helps,

RSL

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [October 21, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/making-expire-fragment-available-to-a-model/117844/3 "2007-10-21T16:25:03Z")

</div>

Thanks Russell. I am already using sweepers however. What I can’t get  
to work is to refactor repetitive code from multiple sweepers into one  
common model which can then be referenced from each sweeper. It  
doesn’t have to be a model really, I’m just trying to clean my code  
up!

For example, in my app, I have some active record models; Location,  
Page, Talent. Each has a sweeper to expire caches but there’s a lot of  
repetition between those three sweepers. I can’t combine them into one  
sweeper because they’re slightly different but I don’t want to keep  
copying & pasting the same expire\_fragment snippets from one to the  
other… seems so un-rails like.

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [October 21, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/making-expire-fragment-available-to-a-model/117844/4 "2007-10-21T18:27:26Z")

</div>

Ahhh, that sorted it… Cheers for the help Russell.

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [October 21, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/making-expire-fragment-available-to-a-model/117844/5 "2007-10-21T17:27:42Z")

</div>

What I’d do is create a module for that shared functionality and mix  
it in to the sweepers.

RSL
