0

ActiveRecord バージョン 3.2.14 (および 3.2.15 rc も試しました) を TinyTDS + activerecord SQL アダプターと共に使用して、SQL Server データベースに接続しています。

この例外がスローされる非常に奇妙な動作に遭遇しています:

/usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record
/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection':
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)

しかし、トレースによると:

from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/model_schema.rb:229:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/model_schema.rb:249:in `column_names'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/model_schema.rb:262:in `column_methods_hash'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/dynamic_matchers.rb:74:in `all_attributes_exists?'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/dynamic_matchers.rb:27:in `method_missing'
from /usr/local/debugging-exchange-test/app/controllers/new_orders_controller.rb:15:in `block in start'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/relation/delegation.rb:6:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/relation/delegation.rb:6:in `each'
from /usr/local/debugging-exchange-test/app/controllers/new_orders_controller.rb:13:in `start'
from /usr/local/debugging-exchange-test/lib/exchange.rb:57:in `testing'
from /usr/local/debugging-exchange-test/lib/exchange.rb:39:in `initialize'
from /usr/local/debugging-exchange-test/lib/exchange.rb:104:in `new'
from /usr/local/debugging-exchange-test/lib/exchange.rb:104:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:203:in `load'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:203:in `start_load'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:298:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/controller.rb:70:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons.rb:147:in `block in run'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:in `catch_exceptions'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons.rb:146:in `run'
from ./data_exchange:15:in `<main>'

メソッドでここで起こっていますeach

def start
  web_queue = Nti::Order.where("orderid IS NOT NULL AND updated_by_exchange_at IS NULL AND orderstatus <> 9")
  logger.info "web_queue size: #{web_queue.length}"
  web_queue.each do |nti_order| #<---- this line
    # logger.info nti_order.orderid
    web_order = Web::Order.find_by_id(nti_order.orderid)
    raise "Couldn't find web order with ID #{nti_order.orderid}" if web_order.nil?
    web_order.status = 99
    web_order.save!
    logger.info "Web order with ID #{web_order.id} updated to status = 99"
  end
end

そして、これが起こる前に、同じ正確なエラーが発生していましたが、この行では.sizeメソッドが原因でした:

logger.info "web_queue size: #{web_queue.size}"

に変更する.lengthと動作します。一体何が起こっているのですか?これらのメソッドは ActiveRecord とは関係ありません。Array メソッドが ActiveRecord 例外をスローしています。

ところで、これはアクティブレコードを使用して 2 つのデータベース間でデータを交換する Ruby アプリ (レールではありません) です。

そして、これを行う方法を変更すると、データベース接続が機能することを示すためだけに:

def start
  a = Nti::Order.where("orderid IS NOT NULL AND updated_by_exchange_at IS NULL AND orderstatus <> 9")
  logger.info a.first.inspect
  logger.info "done"
end

できます。これも同様に機能します:

def start
  a = Nti::Order.where("orderid IS NOT NULL AND updated_by_exchange_at IS NULL AND orderstatus <> 9")
  # logger.info a.size
  puts a.map{ |order| order.orderid }

  logger.info "done"
end

何が起こっているか知っている人はいますか?

4

1 に答える 1