时间gt查询无法在轨道上使用mongoid和ruby

时间:2014-03-26 11:44:14

标签: ruby-on-rails ruby mongoid

我在轨道上使用mongoid和ruby。

我使用

存储mongoid的created_at时间
include Mongoid::Timestamps

所以,让我说我有3个这样的帖子

{"post":"1","created_at": "2014-03-25 13:04:43"}
{"post":"2","created_at": "2014-03-25 13:04:44"}
{"post":"3","created_at": "2014-03-25 13:04:45"}

现在我想获取{"post":"2"}之后创建的所有帖子,即"2014-03-25 13:04:44"之后的帖子,以便只返回{"post":"3"}

Model.where(:created_at.gt => "2014-03-25 13:04:44")

但是上述查询会返回{"post":"2"}{"post":"3"},因此{"post":"2"}不应该在那里。

.lt查询也按预期工作。

任何想法为什么会这样?

提前致谢。

1 个答案:

答案 0 :(得分:3)

问题出在时区...... mongoid created_at字段存储在UTC时区

您可以使用以下工作

Model.where(:created_at.gt => post_2_instance.created_at)

鉴于post_2_instance{"post":"2","created_at": "2014-03-25 13:04:44"}

但如果您想使用日期查询,可以使用以下

Model.where(:created_at.gt => DateTime.parse("2014-03-25 13:04:43").in_time_zone('UTC'))

将奇怪的行为报告给this JIRA issue