Getting typed results from ActiveRecord raw SQL

后端 未结 6 2032
Happy的楠姐
Happy的楠姐 2020-12-29 20:51

In Sequel, I can do:

irb(main):003:0> DB[\"select false\"].get
=> false

Which returns a false boolean. I\'d like to be able to do so

6条回答
  •  滥情空心
    2020-12-29 21:28

    Don't have enough reputation points to respond, but Bjorn's answer and associated replies are broken in Rails 5. This works:

    res = ActiveRecord::Base.connection.select_all(sql)
    res.to_a.map{|o| o.each{|k, v| o[k] = res.column_types[k].cast v}}
    

提交回复
热议问题