(rails3.2.12)
問題
明日の 00:00:00 より前のdbを取得したい
(2013-03-29 00:00:00 を希望)
irb> Time.now => 2013-03-28 11:26:42 +0900 irb> User.where('hoge_at < ?', Time.now.tomorrow.beginning_of_day) User Load (0.7ms) SELECT `users`.* FROM `users` WHERE (hoge_at < '2013-03-28 15:00:00')
2013-03-28 15:00:00
になってしまう。
irb> Time.now.tomorrow.beginning_of_day => 2013-03-29 00:00:00 +0900 irb> Time.now.tomorrow.beginning_of_day.utc => 2013-03-28 15:00:00 UTC
対策
config/application.rb
config.time_zone = 'Tokyo' config.active_record.default_timezone = :local #<-これいれる
確認
irb> User.where('hoge_at < ?', Time.now.tomorrow.beginning_of_day) User Load (0.7ms) SELECT `users`.* FROM `users` WHERE (hoge_at < '2013-03-29 00:00:00')
おけ