ActiveRecord默认时区无法从数据库读回

时间:2015-11-07 00:15:12

标签: ruby-on-rails ruby activerecord timezone rails-activerecord

我试图让所有模型使用EST时区在DB2数据库中写入时间戳,该数据库也配置为EST。

我已使用以下内容配置了application.rb配置:

config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)' 

当我创建新记录并保存到数据库时,时间戳看起来很好:

=> #<TZTestModel id: 3500, added_ts: nil, ...>
irb(main):002:0> rec.added_ts = Time.zone.now
=> Fri, 06 Nov 2015 19:03:42 EST -05:00
irb(main):003:0> rec.added_ts
=> Fri, 06 Nov 2015 19:03:42 EST -05:00
irb(main):004:0> rec.save
SQL (91.0ms)  INSERT INTO TZTESTMODEL(id, added_ts, ...) VALUES (3500, '2015-11-06 19:06:55.237000', ...)
=> true

我查询数据库,我可以看到插入的记录带有正确的时间戳。但是,当我尝试通过ActiveRecord读取记录时,时间戳字段为nil:

irb(main):008:0> TZTestModel.first
=> #<TZTestModel id: 3500, added_ts: nil, ...>

只有在application.rb中设置默认时区时才会出现这种情况。如果我不添加配置,则AR默认为UTC,并且可以写入和读取时间戳字段,而不会出现任何问题。

请非常感谢任何建议。

1 个答案:

答案 0 :(得分:0)

我终于找到了答案。问题是/*Below is a color guessing game that is supposed to run in browser but when I open the code below in my Chrome, nothing happens. Any ideas? I would appreciate any help.*/ /*Below is a color guessing game that is supposed to run in browser but when I open the code below in my Chrome, nothing happens. Any ideas? I would appreciate any help.*/ var colors = ["Aqua", "Black", "Blue", "Brown", "Coral", "Crimson", "Cyan","Fuchsia", "Gold", "Gray", "Green"]; var target; var guess_input_text; var guess_input; var finished = false; var guesses = 0; function do_game() { var random_number = Math.random() * colors.length - 1; var target_index = Math.floor(random_number); target = String(colors[target_index]); //what is wrong below? Why is this not working in my Chrome browser. while (!finished) { guess_input_text = prompt("I am thinking of these colors:\n\n" "Aqua, Black, Blue, Brown, Coral, Crimson, Cyan, Fuchsia, Gold, Gray, Green\n\n" "What color am I thinking of?"); guess_input = String(guess_input_text); guesses += 1; finished = check_guess(); } } /* Is my first if statement correct? Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?*/ function check_guess(){ if (guess_input != "Aqua"||"Black"||"Blue"||"Brown"||"Coral"||"Crimson"||"Cyan"||"Fuchsia"||"Gold"||"Gray", "Green") { alert("Sorry, I don’t recognize your color.\n\nPlease try again."); } if(guess_input < target){ alert("Sorry, your guess is not correct!\n\nHint: your color is alphabetically higher than mine."); return false; } if(guess_input > target){ alert("Sorry, your guess is not correct!\n\nHint: your color is alphabetically higher than mine."); return false; } alert("Congratulations! You have guessed the color!\n\n" "It took you " + guesses + " to finish the game!\n\n" "You can see the color in the background."); return true; } 在RoR下运行时只接受两个值/符号:config.active_record.default_timezone:utc。 我将值设置为:local,它可以完美地获取时区。 :local中的完整配置现在是:

application.rb

当时间戳添加到模型的对象时,模型仍然以UTC格式存储该值,但它会在写入数据库之前和读取时自动转换为EST(例如,在输出为字符串之前)。

我还有一个我的应用程序,它是一个用Ruby编写的守护进程(不是RoR)。在这个守护进程中,我将AR默认时间戳设置为EST而不是:local,我还将ENV [&#39; TZ&#39;]值设置为&#39; EST&#39;,以确保任何日期/在AR之外完成的时间操作也使用正确的时区,如下所示:

config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local 

这使得我的应用程序(RoR应用程序和Ruby守护程序)两侧的时区保持一致。