rails找不到database.yml

时间:2011-06-21 03:49:43

标签: ruby-on-rails mongoid

我刚删除了database.yml,因为我正在尝试使用Mongoid,现在我得到以下内容:

$ rails server           
=> Booting WEBrick
=> Rails 3.0.9 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application/configuration.rb:88:in `read': No such file or directory - /home/chris-kun/code/thirsty/config/database.yml (Errno::ENOENT)
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application/configuration.rb:88:in `database_configuration'
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/railtie.rb:58:in `block (2 levels) in <class:Railtie>'
        from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
        from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
        from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
        from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/railtie.rb:57:in `block in <class:Railtie>'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `instance_exec'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `run'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:50:in `block in run_initializers'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `each'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `run_initializers'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:134:in `initialize!'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:77:in `method_missing'
        from /home/chris-kun/code/thirsty/config/environment.rb:5:in `<top (required)>'
        from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
        from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `block in require'
        from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `block in load_dependency'
        from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in `new_constants_in'
        from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `load_dependency'
        from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
        from /home/chris-kun/code/thirsty/config.ru:3:in `block in <main>'
        from /usr/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/builder.rb:46:in `instance_eval'
        from /usr/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/builder.rb:46:in `initialize'
        from /home/chris-kun/code/thirsty/config.ru:1:in `new'
        from /home/chris-kun/code/thirsty/config.ru:1:in `<main>'
        from /usr/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/builder.rb:35:in `eval'
        from /usr/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/builder.rb:35:in `parse_file'
        from /usr/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/server.rb:162:in `app'
        from /usr/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/server.rb:253:in `wrapped_app'
  1 #!/usr/bin/env ruby
        from /usr/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/server.rb:204:in `start'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands/server.rb:65:in `start'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:30:in `block in <top (required)>'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:27:in `tap'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:27:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

有什么想法吗?

4 个答案:

答案 0 :(得分:14)

config/application.rb中,删除行require 'rails/all'并替换为:

require "rails"

%w(
  action_controller
  action_mailer
  active_resource
  rails/test_unit
).each do |framework|
  begin
    require "#{framework}/railtie"
  rescue LoadError
  end
end

(这是railties gem中rails/all.rb的内容,但删除了activerecord行。)

答案 1 :(得分:4)

之前我遇到过类似问题,但事实证明我有一些分散的active_record引用。完全归功于Dylan Markow的这一个!

Removing database.yml when using Mongoid in Rails 3.2

执行grep -r active_record config/查找所有引用,然后将其注释掉!

答案 2 :(得分:3)

对于那些想要与mongoid一起使用db相关函数ex)rake db:seed的人,还要添加mongoid railtie:

https://github.com/mongodb/mongoid/tree/master/lib/mongoid/railties

require "rails"

# Import gems in Gemfile
Bundler.require(*Rails.groups)

# important that this is imported after gems in Gemfile are imported
%w(
  mongoid
  action_controller
  action_mailer
  active_resource
  rails/test_unit
).each do |framework|
begin
  require "#{framework}/railtie"
    rescue LoadError
  end
end

答案 3 :(得分:3)

Rails 5和所有未来版本的Rails的更新答案:

您应该使用rails/all.rb文件中的require片段,该文件是您的Rails版本的最新版本,而不是active_record栏。 Here is a link to rails/all.rb on the Rails master branch.

在写这篇文章时,它包含了许多你不想错过的新栏目:

require "rails"

%w(
  action_controller/railtie
  action_view/railtie
  action_mailer/railtie
  active_job/railtie
  action_cable/engine
  rails/test_unit/railtie
  sprockets/railtie
).each do |railtie|
  begin
    require railtie
  rescue LoadError
  end
end