为数据库适配器指定'mysql2',但未加载gem

时间:2015-09-28 00:44:28

标签: ruby-on-rails mysql2

我知道很多次都会问这个问题。我也尝试过在互联网上写的所有内容来修复它,但我不能。

当我运行rails generate controller命令时,我在命令行中收到此错误。顺便说一下,我曾经在浏览器中得到这个错误,但是用4.2.2而不是4.2.4创建了项目,我没有在浏览器中看到这个错误。

这是Gemfile。

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.2'
# Use mysql as the database for Active Record
gem 'mysql2', '~> 0.3.18'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Twitter's Bootstrap, converted to Sass and ready to drop into Rails or Compass
gem 'bootstrap-sass', '~> 3.3.5.1'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

捆绑安装:

Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.1
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.2
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile 0.6.2
Using nokogiri 1.6.6.2
Using rails-deprecated_sanitizer 1.0.3
Using rails-dom-testing 1.0.7
Using loofah 2.0.3
Using rails-html-sanitizer 1.0.2
Using actionview 4.2.2
Using rack 1.6.4
Using rack-test 0.6.3
Using actionpack 4.2.2
Using globalid 0.3.6
Using activejob 4.2.2
Using mime-types 2.6.2
Using mail 2.6.3
Using actionmailer 4.2.2
Using activemodel 4.2.2
Using arel 6.0.3
Using activerecord 4.2.2
Using execjs 2.6.0
Using autoprefixer-rails 6.0.3
Using bcrypt 3.1.10
Using debug_inspector 0.0.2
Using binding_of_caller 0.7.2
Using sass 3.4.18
Using bootstrap-sass 3.3.5.1
Using byebug 6.0.2
Using coffee-script-source 1.9.1.1
Using coffee-script 2.4.1
Using thor 0.19.1
Using railties 4.2.2
Using coffee-rails 4.1.0
Using multi_json 1.11.2
Using jbuilder 2.3.1
Using jquery-rails 4.0.5
Using mysql2 0.3.20
Using bundler 1.10.6
Using sprockets 3.3.5
Using sprockets-rails 2.3.3
Using rails 4.2.2
Using rdoc 4.2.0
Using tilt 2.0.1
Using sass-rails 5.0.4
Using sdoc 0.4.1
Using spring 1.4.0
Using turbolinks 2.5.3
Using uglifier 2.7.2
Using web-console 2.2.1
Bundle complete! 14 Gemfile dependencies, 56 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

我该怎么办?谢谢。

编辑:

我确实尝试过:gem'mysql2','〜&gt; 0.3.18'但不起作用。

编辑(错误)

我用...更改了用户名

/Users/.../.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec': Specified 'mysql2' 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 /Users/.../.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/connection_handling.rb:50:in `establish_connection'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/lazy_load_hooks.rb:44:in `each'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/base.rb:316:in `<module:ActiveRecord>'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/base.rb:26:in `<top (required)>'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:319:in `active_record_configured?'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:251:in `disconnect_database'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:97:in `preload'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:143:in `serve'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:131:in `block in run'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:125:in `loop'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:125:in `run'
        from /Users/.../.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application/boot.rb:18:in `<top (required)>'
        from /Users/.../.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /Users/.../.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from -e:1:in `<main>'

修改

我的系统中有3个版本的mysql2。我删除了0.4.1和0.3.20。现在我只有0.3.18,在运行bundle install之后,它说“使用0.3.18”。好吧,但是当我运行时,生成控制器我得到了这个错误:

Could not find mysql2-0.4.1 in any of the sources
Run `bundle install` to install missing gems.

所以看起来rails试图使用0.4.1,即使我告诉它使用0.3.18。

1 个答案:

答案 0 :(得分:0)

如果您更改Gemfile以降级特定的gem版本,请按照以下步骤进行确保工作:

  1. 删除Gemfile.lock文件:

    rm Gemfile.lock
    
  2. 使用Bundle update:

    生成一个新的Gemfile.lock
    bundle update