Sachin srinivasan
Sachin srinivasan

Reputation: 828

Rails raw query values along with fields name

This is my query

@pg = ActiveRecord::Base.connection
result = @pg.execute("select sum(col1) AS col1, sum(col2) AS col2 from messages")

now

result.values gives me [[val1, val2]]

result.fields gives [col1, col2]

is there a way we can get result similar to this ?

{col1 => val1, col2 => val2}

i looked into many solutions.. no luck :(

Upvotes: 0

Views: 32

Answers (1)

krishna raikar
krishna raikar

Reputation: 2675

Try this

@pg = ActiveRecord::Base.connection
result = @pg.execute("select sum(col1) AS col1, sum(col2) AS col2 from messages").first

Upvotes: 1

Related Questions