# Memoization on freeze

**URL:** <https://www.ruby-forum.com/t/memoization-on-freeze/155044>\
**Category:** Rails\
**Created:** [December 6, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/memoization-on-freeze/155044 "2008-12-06T21:46:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [December 6, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/memoization-on-freeze/155044/1 "2008-12-06T21:46:16Z")

</div>

Hey alI,

I like the new support for memoizable methods in Rails 2.2. However,  
I’m running into a problem that’s making it hard to use in my case.

My ActiveRecord model has a method that does a lot of data crunching,  
and it takes a few seconds. I used ActiveSupport::Memoizable and  
memoized that long-running method, which made my code quite clean  
since I can call it willy-nilly from all over my view. Very nice.

The problem happens when I call #destroy on the model. I didn’t know  
much about freezing AR objects, so I was surprised when I found out  
that #destroy called #freeze, and #freeze called #memoize\_all. This  
calls my long-running method, sadly, which is about the last thing I  
want to happen when destroying my object!

My work-around was to call ModelClass.delete(id), but that’s kinda  
lame given that it doesn’t go through the normal callback cycle - it  
will surely bite me on the ass someday.

Any other workaround to this problem I don’t see? Besides, of course,  
rolling my own memoization?

Thanks!  
Mike

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [December 7, 2008, 4:31am UTC](https://www.ruby-forum.com/t/memoization-on-freeze/155044/2 "2008-12-07T04:31:46Z")

</div>

Hey Mike,

That looks a valid issue/bug. I’ve pinged Josh about it as he is the  
guy in charge of memoization code.

Thanks!

On Sat, Dec 6, 2008 at 9:00 PM, Mike G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Any other workaround to this problem I don’t see? Besides, of course,  
> rolling my own memoization?
> 
> Thanks!  
> Mike
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)
