" UserActionEvent没有连接池"连接到SqlServer时

时间:2016-09-21 02:38:27

标签: ruby-on-rails sql-server ruby activerecord

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

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'

my 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

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

1 个答案:

答案 0 :(得分:0)

不确定这里的用例是什么,但是我认为你遇到的问题与establish_connection方法的使用有关(来自ActiveRecord::Base你应该将参数传递给({1}})而不是分配它)

  establish_connection = db_config['Analytics']

应该成为:

  establish_connection db_config['Analytics']