2014-02-23 68 views
0

當我們在時區中嘗試在Rails 3中執行範圍時,我們看到了一些時髦的東西。假設我們有一個叫做日期時間類型的start_at屬性。在application.rb中,我們根據服務器位置設置了config.time_zone = 'Eastern Time (US & Canada)'rails,postgres,時區和範圍的問題

當某人將數據庫中的start_at設置爲'2014-02-22 at 7pm EST'時,其存儲爲'2014-02-23 01:00:00-5',所以當我設置一個命名範圍如:

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

該記錄被錯誤地返回。如果我嘗試這樣的事情:

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

它仍然無法正常工作。如何創建與佔本地時區數據庫日期時間比較範圍,而不必做一些可笑的,比如:

scope :tomorrow, where("date(start_at - INTERVAL '5 hours') = ?", Date.tomorrow) 

回答

0

您必須使用Time.zone選項可以使用您配置

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

我推薦一下Rails time zones

這個好文章