mercredi 21 novembre 2018

calculate time difference in rails postgresql query

I need to get time difference as query result in my rails(which has Postgres database)

Brute Force Approach

I queried all items from my database and then iterating each record and then calculating time difference in hours which is very slow.

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
I need to calculate the difference of time(hours) in query itself, how to achieve that

like - ImageRetouchItem.where(:status => '0').select('(Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round')

Aucun commentaire:

Enregistrer un commentaire