我的 gemfile 有这样一行:
gem 'mysql2', group: :production
但是当我从命令行运行命令时出现此错误:rails r "puts Rails.env"
/Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/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)
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:50:in `establish_connection'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:27:in `each'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `each'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `call'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
...
rest of stack trace
附带说明:我一直无法弄清楚为什么mysql2不会安装。我已经尝试了每个堆栈溢出搜索的几个修复程序,但没有骰子。
更新: 我没有提到,但我想在开发环境中。
当你运行rails r
而不告诉哪个环境时,Rails将使用development
环境作为回退。
您有三种选择:
-
生产环境中的启动导轨
bundle exec rails runner -e production "puts Rails.env"
或
通过 从
Gemfile
的gem 'mysql2'
行中删除, group: :production
,在所有环境中安装mysql2
gem。或
更新
config/database.yml
以在开发环境中使用sqlite3
而不是mysql2
。
您可能会收到错误,因为当您不在生产环境中时,没有数据库驱动程序。