ruby-on-rails - rails 想使用 PG 但指定了 mysql

标签 ruby-on-rails ruby gem

有一个工作的 rails 应用程序并得到一个奇怪的错误 - 即使指定了 mysql 适配器,rails 也想使用 postgresql。在我处理另一个使用 postgresql 的项目之前,一切都 100% 正常。

但是,这仍然没有任何意义。我已经研究了几个小时了。也许我只是瞎了眼,我会很感激你的帮助。

本地测试的错误消息:

bash-3.2$ rails s
=> Booting WEBrick
=> Rails 4.0.3 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/connection_specification.rb:58:in `rescue in resolve_hash_connection': Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile. (Gem::LoadError)
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/connection_specification.rb:55:in `resolve_hash_connection'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/connection_specification.rb:46:in `resolve_string_connection'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/connection_specification.rb:32:in `spec'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_handling.rb:39:in `establish_connection'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/railtie.rb:176:in `block (2 levels) in <class:Railtie>'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.3/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.3/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.3/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.3/lib/active_support/lazy_load_hooks.rb:27:in `each'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.3/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/railtie.rb:174:in `block in <class:Railtie>'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/initializable.rb:30:in `run'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/mg/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from /Users/mg/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
    from /Users/mg/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from /Users/mg/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from /Users/mg/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from /Users/mg/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from /Users/mg/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/application.rb:215:in `initialize!'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/mg/Dropbox/delightotet/delighto/config/environment.rb:16:in `<top (required)>'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/polyglot-0.3.4/lib/polyglot.rb:65:in `require'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/polyglot-0.3.4/lib/polyglot.rb:65:in `require'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:229:in `block in require'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:229:in `require'
    from /Users/mg/Dropbox/delightotet/delighto/config.ru:3:in `block in <main>'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
    from /Users/mg/Dropbox/delightotet/delighto/config.ru:in `new'
    from /Users/mg/Dropbox/delightotet/delighto/config.ru:in `<main>'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/commands/server.rb:48:in `app'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/commands/server.rb:75:in `start'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/commands.rb:76:in `block in <top (required)>'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/commands.rb:71:in `tap'
    from /Users/mg/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.3/lib/rails/commands.rb:71:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

数据库.yml

development:
  adapter: mysql2
  database: xxx
  user: xxx 
  password:

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql2
  database: xxx
  user: xxx 
  password:

production:
  adapter: mysql2
  database: xxx
  user: xxx 
  password: xxx

我的 gem 文件:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'
gem 'mysql2'

gem 'sprockets', '2.11.0'
gem 'mail_form'

# Use SCSS for stylesheets
gem 'sass-rails'

# DROPBOX
gem 'dropbox-sdk'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails'

#mailchimp
gem 'gibbon'
gem 'mandrill-api'

gem 'net-ssh', '~>2.7.0'

#devise auth 
gem 'devise'

# SUPPORT PROCESSES
gem "zendesk_api"

# AFTER SALES PROCESSES

# SHIPPING SOLUTIONS
gem 'dhl-intraship'

# GENERATE PDF INVOICE
gem 'spree_print_invoice' , :git => 'https://github.com/mgreschke/spree_print_invoice.git', branch:'2-2-stable'


# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'gritter'
gem 'friendly_id'

# 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', '~> 1.2'

#password encryption
gem "bcrypt-ruby", :require => "bcrypt"
gem 'money', '6.0.1'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

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

# Use Capistrano for deployment
gem "capistrano", "2.14.2",  group: :development
gem "mime-types", "1.25"
gem "rake", "10.3.1"

#meta tags
gem 'meta-tags'
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'spree', '2.2.1'
gem 'spree_gateway', :git => 'https://github.com/spree/spree_gateway.git', :branch => '2-2-stable'
gem 'spree_i18n', :github => 'spree/spree_i18n', :branch => '2-2-stable'

最佳答案

如果您有一个名为 DATABASE_URL 的环境变量,那么 Rails 将使用它。

这记录在 rails guides 中它特别指出 rails 将尝试将 database.yml 与环境变量合并,并优先考虑环境变量。

我认为作为您提到的这些其他 postgres 项目之一的一部分,您已经设置了这个环境变量。如果您在 shell 提示符下运行 env,那么您应该会看到一个环境变量列表。如果 DATABASE_URL 存在并且在您打开/关闭终端窗口后仍然存在,那么您可能将其设置在您的 shell 启动文件之一中(例如,对于 bash,请尝试 .bashrc、.bash_profile)

关于ruby-on-rails - rails 想使用 PG 但指定了 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25469754/

相关文章:

ruby-on-rails - Bundler Gem 是否考虑了您的 Ruby 环境?

ruby-on-rails - 在 Linux 上安装 ruby​​-filemagic gem 时缺少库

ruby-on-rails - 如何在不使用ruby数据库的情况下保存变量

ruby - Ruby 中的内省(introspection)

ruby-on-rails - 将 Rails 4 App 部署到 Heroku redis 时出错

ruby-on-rails - 使用 twilio 将视频 url 添加到短信或彩信

ruby-on-rails - 下拉框 - 在表单中填充来自另一个表的数据 - Ruby on Rails

ruby-on-rails - ROR 3.1 : Bundle update fails (eventmachine gem)

ruby-on-rails - 如何使Rails 3.1在开发模式下连接 Assets ?

rubygems - bundle 更新 : env: ruby_executable_hooks: No such file or directory