.where created_at time匹配另一个created_at - format .where(created_at)

时间:2014-10-15 14:39:35

标签: ruby-on-rails

我正在尝试使用.where created_at等于另一个created_at来查询数据库。

object_2 = Model.where(created_at: object_1.created_at)

The Model's created_at = "2014-10-15 13:31:58.654969"
object_1's created_at = "2014-10-15 13:31:58.444636"

问题是毫秒是不同的。我如何只匹配DateTime? 2014-10-15 13:31:58

“解决方案”

为了安全,我最终选择了一个范围

Model.where(created_at: object_1.created_at - 1.minute..object_1.created_at + 1.minute)

1 个答案:

答案 0 :(得分:0)

如果你只是想截断毫秒,你可以这样做:

object_2 = Model.where(created_at: object_1.created_at.strftime('%Y-%m-%d %H:%M:%S'))