2016-09-21 93 views
1

我有一個簡單的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

如何處理這個問題? 謝謝!

回答

0

不知道該用例是什麼在這裏,但儘管如此,我以爲你是關係到使用establish_connection方法的問題(從ActiveRecord::Base你應該傳遞一個參數(而不是分配給它的)

establish_connection = db_config['Analytics'] 

應該變成:

establish_connection db_config['Analytics'] 
+0

感謝您的回答 我不得不放棄這個問題 – dodoliu

+0

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