Sunday, 15 April 2012

postgresql - rails 3.2 / postgres - how cast SUM() to return an integer not string? -



postgresql - rails 3.2 / postgres - how cast SUM() to return an integer not string? -

postgres aggregates sum(foo) homecoming string, if foo integer.

how cast sum integer resulting array of relations contains integer not string?

i tried cast using ::integer

widget.select("sum(points) totalpoints::integer, agent, company")

but postgres throws error pg::error: error: syntax error @ or near "::"

it seems there should way tell rails - short of iterating through each returned array element - sum of int int?

this might activerecord adapter. i'm not exclusively sure that, though. think when go downwards selection-level apis returns strings, because that's reads database. when utilize higher-level apis think knows how transform ruby types because it's aware of schema there.

i'm having problem reproducing query above, did following:

widget.connection.select_rows("select sum(id) widgets")

this returned:

[ [0] [ [0] "887" ] ]

and if utilize higher-level api:

2.0.0-p353 :034 > widget.sum('id') d, [2014-06-24t15:34:52.644852 #95176] debug -- : (0.5ms) select sum("widgets."."id") sum_id "widgets" 887

at point it's giving me integer type in console. suspect there's nil wrong postgres, may need convert native types when you're using activerecord select apis.

ruby-on-rails postgresql rails-activerecord

No comments:

Post a Comment