2015-11-08 38 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

相關問題