2

我正在使用 MySQL 和 ruby​​ 1.8.6。我想连接到 MySQL,因此我正在使用活动记录 gem。我的代码是

def initialize

    @db_adapter  = "mysql2"
    @db_host     = "localhost"
    @db_database = "database_name"
    @db_user     = "root"
    @db_password = "root123"
    con = nil

end

def database_connection
           con = ActiveRecord::Base.establish_connection(  
              :adapter  => $db_adapter,  
              :host     => $db_host,  
              :database => $db_database,
              :user     => $db_user,
              :password => $db_password  
           )

           return con
end

但是当我试图连接时,它给了我这个错误

/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:76:in `establish_connection': Please install the  adapter: `gem install activerecord--adapter` (no such file to load -- active_record/connection_adapters/_adapter) (RuntimeError)
    from /cmf/samnew/build/Controller/databaseConnection.rb:32:in `database_connection'
    from controller.rb:23
4

2 回答 2

3

该错误显示activerecord--adapter未安装 gem。您可以使用以下命令安装 gem

gem install activerecord--adapter.
于 2012-11-02T04:26:17.937 回答
0

尝试

con = ActiveRecord::Base.establish_connection(  

        :adapter  => @db_adapter,  

        :host     => @db_host,  

        :database => @db_database,

        :user     => @db_user,

        :password => @db_password  

)
于 2012-09-20T07:22:47.273 回答