0
我在Rails中使用原生查詢(因爲我的查詢很複雜,我不認爲它可以在ActiveRecord
中表示)。所以這裏是我如何嘗試:Rails:PG :: ConnectionBad(連接已關閉)
get do
connection = ActiveRecord::Base.connection.raw_connection
connection.prepare('order_statistic', @@sql)
data = connection.exec_prepared('order_statistic', [params[:id]])
connection.close()
data
end
此代碼第一次成功運行。但第二次,它會拋出異常。 (我正在使用PostgresSQL)
PG::ConnectionBad (connection is closed)
如果我刪除行connection.close
。我會遇到另一個例外:
PG::DuplicatePstatement (ERROR: prepared statement "order_statistic" already exists
請幫助我如何正確使用原始查詢。
感謝