开发者

In Ruby on Rails, non-deterministic data returned from DB?

开发者 https://www.devze.com 2023-02-22 00:36 出处:网络
I tried a line: last_item = Item.last last_item.created_at.strftime(\'%Y-%m-%d\') and the SQL in the console log is:

I tried a line:

last_item = Item.last
last_item.created_at.strftime('%Y-%m-%d')

and the SQL in the console log is:

SELECT "items".* FROM "items" ORDER BY items.id DESC LIMIT 1

BUT sometimes I get 2011-04-03, and sometimes, 2011-04-04

that is really strange. If I print the time out, it is

Sun, 03 Apr 2011 17:58:48 UTC +00:00

what give Rails 3 (3.0.5) this non-deterministic behavior? Is it Time Zone? But I am pressing Reload in the browser and it gives 04-03 this time and next time it gives 04-04 without restarting the server or changing any config file or source code.


Update: further debugging show that when it is 04-04, then the time printed using p last_item.created_at is:

Mon, 04 Apr 2011 01:58:48 HKT +08:00

but I have this line in the view:

- Time.zone = 'Hong Kong'

but the last_item.created_at.strftime('%Y-%m-%d') was done in the controller. S开发者_StackOverflow中文版o shouldn't the controller run first, and in the view, I am merely trying to print it using a different Time zone... also, why will it sometimes become UTC, and sometimes HKT?


I think that setting the time zone in the view could cause this non-deterministic behavior. You should try setting your time zone somewhere in the config directory, perhaps in config/application.rb.

0

精彩评论

暂无评论...
验证码 换一张
取 消