# Named\_scope and Time.zone

**URL:** <https://www.ruby-forum.com/t/named-scope-and-time-zone/166446>\
**Category:** Rails\
**Created:** [April 25, 2009, 11:52am UTC](https://www.ruby-forum.com/t/named-scope-and-time-zone/166446 "2009-04-25T11:52:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [April 25, 2009, 11:52am UTC](https://www.ruby-forum.com/t/named-scope-and-time-zone/166446/1 "2009-04-25T11:52:12Z")

</div>

So I’m trying to use a named scope to show upcoming events like so:

class Event  
named\_scope :upcoming, :conditions =\> [“starts\_at \>= ?”,  
Time.zone.now.beginning\_of\_day]  
end

In my application controller I am setting up the current time zone in  
a before filter. The times that I display throughout the application  
are all showing up correctly (in the current time zone). The problem  
is that when I use this named scope to do searches it seems to use the  
local system time zone rather than the time zone that I am specifying  
in the application controller. Any idea why this might be occurring?

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [April 25, 2009, 1:23pm UTC](https://www.ruby-forum.com/t/named-scope-and-time-zone/166446/2 "2009-04-25T13:23:01Z")

</div>

I’ve been doing a bit more experimenting and it only happens in  
production. Both boxes are using the same gems (rails 2.3.2). I  
found a temporary solution which was creating a class method named  
“upcoming” and using the same exact conditions.

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [April 25, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/named-scope-and-time-zone/166446/3 "2009-04-25T17:01:55Z")

</div>

On Apr 25, 1:50 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am no expert on this, but since no-one else has responded I will have a  
> go. I think it may be that the date value for your named scope is  
> calculated when the file is loaded, not each time the named scope is used.  
> In development the files are reloaded at each request so all is well. In  
> production they are loaded only once. I think you need to use the lambda  
> syntax in the named scope to force it to recalculate each time.

Spot on, lambdas are the way to go here.

Fred

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [April 26, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/named-scope-and-time-zone/166446/4 "2009-04-26T17:00:55Z")

</div>

_Smacks forehead_ Thanks Colin & Frederick for the responses - it  
makes sense now. I modified to use lambdas and it seems to be working  
as expected:

class Event  
named\_scope :upcoming, lambda {  
{ :conditions =\> [“starts\_at \>= ?”,  
Time.zone.now.beginning\_of\_day] }  
}  
end

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [April 25, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/named-scope-and-time-zone/166446/5 "2009-04-25T14:51:21Z")

</div>

I am no expert on this, but since no-one else has responded I will have  
a  
go. I think it may be that the date value for your named scope is  
calculated when the file is loaded, not each time the named scope is  
used.  
In development the files are reloaded at each request so all is well. In  
production they are loaded only once. I think you need to use the  
lambda  
syntax in the named scope to force it to recalculate each time. See  
[http://workswithruby.com/tags/named\_scope](http://workswithruby.com/tags/named_scope) for example.

2009/4/25 ionlydoeyes [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
