连接到SqlServer时没有“UserActionEvent的连接池”

问题描述:

我有一个简单的ruby脚本。
我需要连接SqlServer和Mysql。我使用gem'mysql2'连接成功,但我使用gem'tiny_tds'和'activerecord-sqlserver-adapter'失败。 我的错误:连接到SqlServer时没有“UserActionEvent的连接池”

C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:570:in `retrieve_connection': No connection pool for UserActionEvent (ActiveRecord::ConnectionNotEstablished) 
    from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.7.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection' 
    from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.7.1/lib/active_record/connection_handling.rb:87:in `connection' 
    from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.7.1/lib/active_record/querying.rb:39:in `find_by_sql' 
    from D:/work/rails_obj/export_data/business/tear_bb.rb:49:in `main' 
    from export_data.rb:22:in `main' 
    from export_data.rb:28:in `<main>' 

我的Gemfile:

gem 'mysql2', '0.4.4' 
gem 'tiny_tds' 
gem 'activerecord-sqlserver-adapter' 
gem 'require_all', '1.3.3' 
gem 'activerecord', '~>4.2.1' 

我Gemfile.lock的:

GEM 
    remote: https://gems.ruby-china.org/ 
    specs: 
    activemodel (4.2.7.1) 
     activesupport (= 4.2.7.1) 
     builder (~> 3.1) 
    activerecord (4.2.7.1) 
     activemodel (= 4.2.7.1) 
     activesupport (= 4.2.7.1) 
     arel (~> 6.0) 
    activerecord-sqlserver-adapter (4.2.15) 
     activerecord (~> 4.2.1) 
    activesupport (4.2.7.1) 
     i18n (~> 0.7) 
     json (~> 1.7, >= 1.7.7) 
     minitest (~> 5.1) 
     thread_safe (~> 0.3, >= 0.3.4) 
     tzinfo (~> 1.1) 
    arel (6.0.3) 
    builder (3.2.2) 
    i18n (0.7.0) 
    json (1.8.3) 
    mini_portile2 (2.1.0) 
    minitest (5.9.0) 
    mysql2 (0.4.4-x64-mingw32) 
    require_all (1.3.3) 
    thread_safe (0.3.5) 
    tiny_tds (1.0.4-x64-mingw32) 
     mini_portile2 (~> 2.0) 
    tzinfo (1.2.2) 
     thread_safe (~> 0.1) 

PLATFORMS 
    x64-mingw32 

DEPENDENCIES 
    activerecord (~> 4.2.1) 
    activerecord-sqlserver-adapter 
    mysql2 (= 0.4.4) 
    require_all (= 1.3.3) 
    tiny_tds 

BUNDLED WITH 
    1.12.5 

我的代码:

class AnalyticsBase < ActiveRecord::Base 
    db_config = YAML::load File.open 'configs/sqlserver.yml' 
    establish_connection = db_config['Analytics'] 
    self.abstract_class = true 
end 

class UserActionEvent < AnalyticsBase 
    self.table_name = 'UserActionEvent' 
end 

class TearBb 
    def self.main 
    sql3 = "select top 5 * from UserActionEvent" 
    UserActionEvent.find_by_sql sql3 
    end 
end 

TearBb.main 

CONFIGS/sqlserver.yml:

comm_config: &comm 
    adapter: sqlserver 
    encoding: utf8 
Analytics: 
    <<: *comm 
    host: myhost 
    port: myport 
    database: mydatabase 
    username: myusername 
    password: mypasword 
    timeout: 60000 

我的系统:Windows 7

如何处理这个问题? 谢谢!

不知道该用例是什么在这里,但尽管如此,我以为你是关系到使用establish_connection方法的问题(从ActiveRecord::Base你应该传递一个参数(而不是分配给它的)

establish_connection = db_config['Analytics'] 

应该变成:

establish_connection db_config['Analytics'] 
+0

感谢您的回答 我不得不放弃这个问题 – dodoliu

+0

因为我cant't找到原因。! – dodoliu