# Time-dependent values/constants in Models: An issue or not?

**URL:** <https://www.ruby-forum.com/t/time-dependent-values-constants-in-models-an-issue-or-not/190748>\
**Category:** Rails\
**Created:** [June 26, 2010, 3:00pm UTC](https://www.ruby-forum.com/t/time-dependent-values-constants-in-models-an-issue-or-not/190748 "2010-06-26T15:00:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 26, 2010, 3:00pm UTC](https://www.ruby-forum.com/t/time-dependent-values-constants-in-models-an-issue-or-not/190748/1 "2010-06-26T15:00:12Z")

</div>

Hi there,

I remember reading in the comments of a Rails blog something along these  
lines:

“Since a Model is loaded only once in production development (at server  
start), certain time-dependent ‘constants’ that you have placed in a  
method of a Model will always return the same value for those  
‘constants’, i.e. the value the ‘constant’ had at server start.”

Hypothetic example for the MySQL ‘constant’ “UTC\_TIMESTAMP()” in a Model  
‘User’:

# ===== def books\_created\_right\_now self.books.find(:all, :conditions =\> “books.updated\_at = UTC\_TIMESTAMP()”) end

Is the above statement utter BS or is there actually some issue similar  
to the example or statement?

Thanks a lot!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 28, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/time-dependent-values-constants-in-models-an-issue-or-not/190748/2 "2010-06-28T18:29:14Z")

</div>

On Jun 26, 2010, at 6:00 AM, Tom Ha wrote:

> Hypothetic example for the MySQL ‘constant’ “UTC\_TIMESTAMP()” in a Model  
> to the example or statement?  
> The above example is perfectly okay. However, the below isn’t:

named\_scope :books\_created\_right\_now, :conditions =\> [“updated\_at = ?”,  
Time.now]

The reason the above fails is that Time.now is evaluated _once_ when the  
class is loaded. To get around that you can use a lambda for the named  
scope.

named\_scope :books\_created\_right\_now, lambda { {:conditions =\>  
[“updated\_at = ?”, Time.now]} }

Or, for example you wouldn’t want to do this:

class Book \<\< AR  
NOW = Time.now

# use NOW somewhere in a method thinking it’s still going to be ‘now’

end

I believe that is what the documents are referring to.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 28, 2010, 8:42pm UTC](https://www.ruby-forum.com/t/time-dependent-values-constants-in-models-an-issue-or-not/190748/3 "2010-06-28T20:42:35Z")

</div>

Thanks a lot for your explanation (I believe a lot of folks are not  
necessarily aware of this “detail”…).

One question back, though:

Is the issue rather…

a) with Rails’ “Time.now” VS MySQL’s “UTC\_TIMESTAMP()” (where the former  
would _never_ return the “true now” when used _anywhere within a Class_  
and the latter would always be returned as the “true now”), or is the  
issue…

b) with the _context_ of the “time constant” (equally in both cases),  
where the constant would always return the “true now” if it is used:

- _within a method_ of a Class, or
- within a named\_scope, together with a lambda,
- but NOT _outside of a method_ within a Class?

You also say:

> The reason the above fails is that Time.now is evaluated _once_ when the  
> class is loaded.

-\> Are a Class’ methods not also evaluated only _once_?

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 28, 2010, 10:28pm UTC](https://www.ruby-forum.com/t/time-dependent-values-constants-in-models-an-issue-or-not/190748/4 "2010-06-28T22:28:45Z")

</div>

Got it - thanks a bunch !

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 28, 2010, 9:28pm UTC](https://www.ruby-forum.com/t/time-dependent-values-constants-in-models-an-issue-or-not/190748/5 "2010-06-28T21:28:25Z")

</div>

On Jun 28, 2010, at 11:42 AM, Tom Ha wrote:

> issue…
> 
> > class is loaded.
> 
> -\> Are a Class’ methods not also evaluated only _once_?

No. The danger is that you might assign the result of Time.now to a  
class variable or constant. Or use it in a spot that _is_ evaluated  
only once (like my first named\_scope example) and not realize it.

* * *

class Foo

NOW = Time.now

def self.klass\_meth  
puts Time.now.to\_s + ’ ’ + NOW.to\_s  
end

def inst\_meth  
puts Time.now.to\_s + ’ ’ + NOW.to\_s  
end

end

Foo.klass\_meth  
sleep 1  
Foo.klass\_meth  
sleep 1

## f = Foo.new sleep 1 f.inst\_meth sleep 1 f.inst\_meth

Mon Jun 28 12:25:36 -0700 2010 Mon Jun 28 12:25:36 -0700 2010  
Mon Jun 28 12:25:37 -0700 2010 Mon Jun 28 12:25:36 -0700 2010  
Mon Jun 28 12:25:39 -0700 2010 Mon Jun 28 12:25:36 -0700 2010  
Mon Jun 28 12:25:40 -0700 2010 Mon Jun 28 12:25:36 -0700 2010
