Invoking a large set of SQL from a Rails 4 application

前端 未结 8 1049
有刺的猬
有刺的猬 2020-12-30 07:11

I have a Rails 4 application that I use in conjunction with sidekiq to run asynchronous jobs. One of the jobs I normally run outside of my Rails application is

8条回答
  •  猫巷女王i
    2020-12-30 07:53

    I had the same problem with a set of sql statements that I needed to execute all in one call to the server. What worked for me was to set up an initializer for Mysql2 adapter (as explained in infused answer) but also do some extra work to process multiple results. A direct call to ActiveRecord::Base.connection.executewould only retrieve the first result and issue an Internal Error.

    My solution was to get the Mysql2 adapter and work directly with it:

    client = ActiveRecord::Base.connection.raw_connection
    

    Then, as explained here, execute the query and loop through the results:

    client.query(multiple_stms_query)
    while client.next_result
      result = client.store_result
      # do something with it ...
    end
    

提交回复
热议问题