0

We're seeing some funky stuff when trying to do scopes in Rails 3 in regards to timezones. Let's say we have an attribute called start_at of type date time. In application.rb we have config.time_zone = 'Eastern Time (US & Canada)' set based on our server location.

When someone sets start_at to something like '2014-02-22 at 7pm EST' in the db it's stored as '2014-02-23 01:00:00-5' so when I setup a named scope such as:

scope :tomorrow, where("start_at = ?", Date.tomorrow)

that record is wrongly returned. If I try something like this:

scope :tomorrow, where("start_at at time zone 'EST' = ?", Date.tomorrow)

it still doesn't work. How can I create scopes with db date time comparisons that account for the local timezone without having to do something ridiculous, such as:

scope :tomorrow, where("date(start_at - INTERVAL '5 hours') = ?", Date.tomorrow)
Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
JoshL
  • 1,397
  • 1
  • 12
  • 28

1 Answers1

0

You have to use the Time.zone option to use the zone that you configured

scope :tomorrow, where("start_at = ?", Time.zone.today + 1) 

I recommend this good article about Rails time zones

Rafa Paez
  • 4,820
  • 18
  • 35