我为mysql2
和Active Record安装了宝石。当我运行我的代码时,我收到一个错误:
/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 :(得分:0)
您可以在项目的根目录下创建Gemfile。这应该允许你使用mysql2 gem。
答案 1 :(得分:0)
对于Active Record 4.2.4,您需要使用以下版本的mysql
gem:
gem "mysql2", "~> 0.3.18"