# Time.now cached in model association?

**URL:** <https://www.ruby-forum.com/t/time-now-cached-in-model-association/165702>\
**Category:** Rails\
**Created:** [April 16, 2009, 3:53pm UTC](https://www.ruby-forum.com/t/time-now-cached-in-model-association/165702 "2009-04-16T15:53:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [April 16, 2009, 3:53pm UTC](https://www.ruby-forum.com/t/time-now-cached-in-model-association/165702/1 "2009-04-16T15:53:36Z")

</div>

I have an object that has\_many widgets, but I only want the widgets that  
are not expired associated with this object. My app is showing widgets  
that are expired, so the date must be getting cached. Anyone got a  
simple workaround for this? Is there some way to expire the cache every  
24 hours, or force it to use the current Time.now every time it looks at  
this association? Any help would be greatly appreciated. I could check  
it in the view or the controller but that seems like a pretty messy  
solution. Let me know if you all have any ideas. Thanks!!

has\_many :widgets, :conditions =\>  
“expiration\>=’#{Time.now.beginning\_of\_day.strftime(”%Y-%m-%d")}’"

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [April 16, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/time-now-cached-in-model-association/165702/2 "2009-04-16T15:55:18Z")

</div>

Note: I am using rails version 1.2.3

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [April 16, 2009, 4:09pm UTC](https://www.ruby-forum.com/t/time-now-cached-in-model-association/165702/3 "2009-04-16T16:09:11Z")

</div>

On 16 Apr 2009, at 14:53, Yanni M. wrote:

> this association? Any help would be greatly appreciated. I could  
> check  
> it in the view or the controller but that seems like a pretty messy  
> solution. Let me know if you all have any ideas. Thanks!!
> 
> has\_many :widgets, :conditions =\>  
> “expiration\>='#{Time.now.beginning\_of\_day.strftime(”%Y-%m-%d")}'"

Caching isn’t really what’s happening. That line is evaluated  
precisely once, and so that string interpolation happens once. I wrote  
a bit about this and similar problems at

> **[Do you know when your code runs? - Space Vatican](https://www.spacevatican.org/2008/11/9/do-you-know-when-your-code-runs/)**
>
> Few things are more head bashing inducing than code that passes all unit tests, runs perfectly on your development machine but fails on your staging/ …

Fred

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [April 16, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/time-now-cached-in-model-association/165702/4 "2009-04-16T16:56:17Z")

</div>

You are the man Fred! Exactly what I needed. Thanks!!
