【发布时间】:2018-09-14 17:18:42
【问题描述】:
我有一个用户和约会数据库。约会具有 TimeWithZone 格式的 start_time 和 end_time。我想确定哪些用户已超过某个总持续时间。不幸的是,约会表没有内置持续时间,所以我必须自己计算。
如果我从 end_time 中减去 start_time,它会返回一个浮点数,这对我来说是完美的。
Appointment.first.end_date - Appointment.first.start_time
=> 3600.0
(Appointment.first.end_date - Appointment.first.start_time).class
=> Float
如果我在整个表上运行它并尝试创建一个新列,它会返回一个字符串:
Appointment.select("end_time - start_time as duration").first.duration
=> "01:00:00"
Appointment.select("end_time - start_time as duration").first.duration.class
=> String
我不确定为什么类型会改变以及如何将其转换回浮点数或任何更容易处理的类。理想情况下,我的最终查询类似于:
User.joins(:appointments).select("users.id", "end_time - start_time as duration").group("users.id").having("SUM(duration) >= ?", timelimit)和timelimit取决于最长持续时间和我得到的单位
1) 为什么对于非常相似的命令会得到不同的结果
2) 如何根据结束和开始时间创建一个新列,表示每个约会的持续时间?
【问题讨论】:
-
start_date 和 start_time 有区别吗?
Appointment.select("end_time - start_time as duration").first返回什么? -
您在 Ruby 中减去
Dates,但在数据库中减去times。
标签: sql ruby-on-rails time rails-activerecord