【发布时间】:2019-04-24 02:02:51
【问题描述】:
我需要在 Rails(具有 Postgres 数据库)中获取时差作为查询结果
Brute Force Approach
我从我的数据库中查询了所有项目,然后迭代每条记录,然后以小时为单位计算时间差,这非常慢。
1) @image_retouch_items = ImageRetouchItem.where(:status => '0') = Retrieved all data
2) @image_retouch_items.each do |retouch_item|
latency_date = ((Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round
end
Optimized
我需要计算查询本身的时间(小时)差异,如何实现
like - ImageRetouchItem.where(:status => '0').select('(Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round')
【问题讨论】:
标签: ruby-on-rails postgresql ruby-on-rails-3 activerecord