Why does Rails 3 with Mysql2 Gem ActiveRecord::Base.connection.execute(sql) return Array not Hash?

前端 未结 5 1393
深忆病人
深忆病人 2021-01-31 22:43

I\'m in the process of upgrading an application to Rails 3. I\'ve decided to go with the mysql2 gem. There\'s some legacy code in the app that makes calls like:

         


        
5条回答
  •  隐瞒了意图╮
    2021-01-31 22:47

    instead of

    result = ActiveRecord::Base.connection.execute(sql)
    

    do

    results = ActiveRecord::Base.connection.exec_query(sql)
    

    And that'll do exactly what you want. In particular,

    results.first
    

    will be a hash, and so on.

    Thanks to @_fx for figuring this out!

    For more, see http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Mysql2Adapter.html#method-i-exec_query

提交回复
热议问题