【发布时间】:2016-11-09 06:30:49
【问题描述】:
Foo.first = {id: 1, start:2000-01-01 07:00:00 UTC, end: 2000-01-01 12:00:00 UTC, total_hours_worked: nil}
这里以end为例。
我已经为start 和end 创建了Foo 和t.time,因为我只需要几小时/分钟。我以前从未使用过 Time 对象,所以请耐心等待。
我需要用start 和end 获得总小时数:
start = Foo.find(1).start
end = Foo.find(1).end
start + end #=> errors
start.to_i + end.to_i = 1893438000 #=> What's that?
distance_of_time_in_words(start.to_i + end.to_i) #=> "about 60 years" What?
我期待5:00:00。我知道5:00:00 确实意味着早上 5 点,但上下文会有所不同。
很少搜索我见过this。我真的很努力不在 Rails/Ruby 中为这种“应该采用的方法”使用更多的宝石。
我的问题将找到我真正想要实现的目标的答案(获得一周的总工作时间):
Foo.pluck(:total_hours_worked).sum(&:to_d) #=> best way?
:total_hours_worked 是一个字符串,只是为了让事情变得不那么复杂。
【问题讨论】:
标签: ruby-on-rails ruby postgresql ruby-on-rails-5