RoR 应用程序中的时间戳未更新
我有一个小网站,可以按提交日期对新闻主题进行排序。在本地,这工作得很好。即使连续快速发布故事,时间戳也会有所不同。
示例:
Submitted Fri Mar 25 14:31:09 2011
Submitted Fri Mar 25 14:30:45 2011
Submitted Fri Mar 25 14:30:23 2011
但是,一旦将代码推送到 heroku 并使用 MongoHQ 设置数据库,秒值似乎会被忽略或冻结。
文档日期时间值示例:
added_on 03/14/2011 09:58 AM
时间戳示例:
Submitted Thu Mar 24 13:48:40 2011
Submitted Thu Mar 24 13:48:40 2011
Submitted Thu Mar 24 13:48:40 2011
Submitted Thu Mar 24 13:48:40 2011
秒值似乎没有更新?
这是模型代码,
class Post
include Mongoid::Document
field :link
field :title
field :synopsis
field :added_on, :type => DateTime, :default => DateTime.now
field :poster
field :category
validates_presence_of :link
validates_presence_of :title
validates_presence_of :synopsis
validates_presence_of :category
validates_uniqueness_of :link
validates_uniqueness_of :title
embeds_many :replies
#referenced_in :topic
end
I have a small site that sorts news topics by date of submission to the second. Locally, this works fine. Even when rapidly posting stories in a row there is a difference in the time stamp.
Example:
Submitted Fri Mar 25 14:31:09 2011
Submitted Fri Mar 25 14:30:45 2011
Submitted Fri Mar 25 14:30:23 2011
However, once the code is pushed to heroku and the database is set up with MongoHQ the seconds value seems ignored or frozen.
Example Document DateTime value:
added_on 03/14/2011 09:58 AM
Example timestamps:
Submitted Thu Mar 24 13:48:40 2011
Submitted Thu Mar 24 13:48:40 2011
Submitted Thu Mar 24 13:48:40 2011
Submitted Thu Mar 24 13:48:40 2011
It appears the seconds value isn't updating?
Here is the model code,
class Post
include Mongoid::Document
field :link
field :title
field :synopsis
field :added_on, :type => DateTime, :default => DateTime.now
field :poster
field :category
validates_presence_of :link
validates_presence_of :title
validates_presence_of :synopsis
validates_presence_of :category
validates_uniqueness_of :link
validates_uniqueness_of :title
embeds_many :replies
#referenced_in :topic
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是您需要将默认值更改为 Proc。
在开发模式下,每次请求都会重新加载您的模型,因此
DateTime.now
始终是最新的。然而,在生产中,该类仅在应用程序启动期间加载一次(每个 dyno),并且DateTime.now
会生成静态值。应该是你想要的。
My guess would be that you need to change the default value into a Proc.
In development mode, your models are being reloaded every request, so
DateTime.now
is always current. In production, however, the class is only loaded once (per dyno) during app startup, andDateTime.now
results in a static value.should be what you want.
您的类中的此声明:
将在读取文件时进行处理。在 Heroku 上,当您推送更新并且编译 slug 时,就会发生这种情况。结果是,无论何时编译 slug,默认值都是固定的。试试这个:
在您的开发环境中一切可能都工作正常,因为文件一直在重新加载。
This declaration in your class:
Will be handled when the file is read. On Heroku, this happens when you push your update and the slug is compiled. The result is that the default value is fixed at whatever the time happens to be when the slug is compiled. Try this instead:
Everything probably works fine in your development environment because the file keeps getting reloaded all the time.