0

I am running a query in rails to get the delay between two events in minutes. I have this working nicely as follows;

orders.select("extract(epoch from (orders.procedure_time - orders.call_time)) / 60 AS delay").map &:delay

where orders is an existing ActiveRecord::Relation object

This works absolutely fine so far except that what I get back is an array of strings rather than integers. It's easy enough to sort out in Ruby - what I have done is the following;

orders.select("extract(epoch from (orders.procedure_time - orders.call_time)) / 60 AS delay").map{|x| x.delay.to_i}

This will do, but I am just wondering if there is a way to get the integers directly from postgres? It seems like it must have the integers initially, but they are being converted to strings which I then have to convert back, which has a whiff of inelegance about it.

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
brad
  • 9,573
  • 12
  • 62
  • 89
  • I guess the pgsql driver is converting the integers to strings. Can you try to execute `"select 1::int as id;"` and see what is the type of the returned result? – Antoan Milkov Sep 15 '13 at 04:00
  • @AntoanMilkov: Anything that isn't a table column will come out as a string. – mu is too short Sep 15 '13 at 04:02
  • possible duplicate of [sums return String, only with postgresql](http://stackoverflow.com/questions/18754007/sums-return-string-only-with-postgresql) or at least this is another face of the same underlying problem. – mu is too short Sep 15 '13 at 04:03
  • OK, thanks. It looks like there's no other way then. – brad Sep 15 '13 at 04:09

0 Answers0