2015-11-08 13 views
0

我安装了mysql2和Active Record的gems。当我运行我的代码我得到一个错误:将`gem'mysql2'`添加到您的Gemfile中,以便在Ruby Nitrouis IO中没有rails项目

/usr/local/opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:177:in` `rescue in spec': Specified 'my sql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError) 
     from /usr/local/opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec' 
     from `/usr/local/opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_handling.rb:50:in `establish_connection' 

这里是我的代码:

require 'mysql2' 
require 'active_record' 

client = Mysql2::Client.new(:host => "localhost", :username => "root") 

client.query("show databases").each do |db| 
    p db 
end 

ActiveRecord::Base.establish_connection(
    adapter: 'mysql2', 
    databases: 'store_manager' 
) 

错误是说我需要我的SQL2添加到我的Gemfile。我在哪里可以找到它,并且有什么我需要添加?

回答

0

您可以在项目的根目录创建一个Gemfile。这应该允许你使用mysql2 gem。

0

对于活动记录4.2.4,你需要使用mysql宝石以下版本:

gem "mysql2", "~> 0.3.18" 
+0

我加入MySQL的我的Gemfile。我仍然收到同样的错误。 – Aaron

+0

@Aaron:请编辑您的问题以包含您的Gemfile,我们来看看。 – Drenmi

相关问题