Rails原始查询值以及字段名称

时间:2017-09-22 05:03:29

标签: ruby-on-rails postgresql activerecord

这是我的查询

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

现在

result.values给了我[[val1, val2]]

result.fields提供[col1, col2]

有没有办法让我们得到与此类似的结果?

{col1 => val1, col2 => val2}

我研究了许多解决方案..没有运气:(

1 个答案:

答案 0 :(得分:1)

试试这个

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