1

我的 mongoid.yml:

 production:
  sessions:
    default:
      database: kvallspressen_production
      hosts:
        - localhost:27017
      options:
    options:

development:
  # Configure available database sessions. (required)
  sessions:
    # Defines the default session. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: kvallspressen_development
      # Provides the hosts the default session can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - localhost:27017
      options:
        # Change whether the session persists in safe mode by default.
        # (default: false)
        # safe: false

        # Change the default consistency model to :eventual or :strong.
        # :eventual will send reads to secondaries, :strong sends everything
        # to master. (default: :eventual)
        # consistency: :eventual

        # How many times Moped should attempt to retry an operation after
        # failure. (default: 30)
        # max_retries: 30

        # The time in seconds that Moped should wait before retrying an
        # operation on failure. (default: 1)
        # retry_interval: 1
  # Configure Mongoid specific options. (optional)
  options:
    # Configuration for whether or not to allow access to fields that do
    # not have a field definition on the model. (default: true)
    # allow_dynamic_fields: true

    # Enable the identity map, needed for eager loading. (default: false)
    # identity_map_enabled: false

    # Includes the root model name in json serialization. (default: false)
    # include_root_in_json: false

    # Include the _type field in serializaion. (default: false)
    # include_type_for_serialization: false

    # Preload all models in development, needed when models use
    # inheritance. (default: false)
    # preload_models: false

    # Protect id and type from mass assignment. (default: true)
    # protect_sensitive_fields: true

    # Raise an error when performing a #find and the document is not found.
    # (default: true)
    # raise_not_found_error: true

    # Raise an error when defining a scope with the same name as an
    # existing method. (default: false)
    # scope_overwrite_exception: false

    # Skip the database version check, used when connecting to a db without
    # admin access. (default: false)
    # skip_version_check: false

    # User Active Support's time zone in conversions. (default: true)
    # use_activesupport_time_zone: true

    # Ensure all times are UTC in the app side. (default: false)
    # use_utc: false
test:
  sessions:
    default:
      database: kvallspressen_test
      hosts:
        - localhost:27017
      options:
        consistency: :strong
        # In the test environment we lower the retries and retry interval to
        # low amounts for fast failures.
        max_retries: 1
        retry_interval: 0

当我跑RAILS_ENV=production rake db:create

我收到此错误:

** Invoke db:create (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
undefined method `has_key?' for nil:NilClass
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid/config/validators/session.rb:99:in `no_database_or_uri?'
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid/config/validators/session.rb:45:in `validate_session_database'
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid/config/validators/session.rb:25:in `block in validate'
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid/config/validators/session.rb:24:in `each_pair'
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid/config/validators/session.rb:24:in `validate'
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid/config.rb:183:in `sessions='
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid/config.rb:213:in `load_configuration'
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid/config.rb:85:in `load!'
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid.rb:146:in `load!'
/usr/local/lib/ruby/gems/2.0.0/gems/mongoid-3.0.23/lib/mongoid/railtie.rb:67:in `block in <class:Railtie>'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `instance_exec'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `run'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:55:in `block in run_initializers'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `run_initializers'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/application.rb:136:in `initialize!'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/railtie/configurable.rb:30:in `method_missing'
/var/www/kvallspressen/config/environment.rb:5:in `<top (required)>'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/application.rb:103:in `require'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/application.rb:103:in `require_environment!'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-3.2.12/lib/rails/application.rb:297:in `block (2 levels) in initialize_tasks'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:228:in `call'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:228:in `block in execute'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:223:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:223:in `execute'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:166:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:187:in `block in invoke_prerequisites'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:185:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:185:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:165:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/task.rb:152:in `invoke'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/application.rb:143:in `invoke_task'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/application.rb:101:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/application.rb:101:in `block in top_level'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/application.rb:110:in `run_with_threads'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/application.rb:95:in `top_level'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/application.rb:73:in `block in run'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/lib/rake/application.rb:70:in `run'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.0.3/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'
Tasks: TOP => db:create => environment

宝石文件:

source 'http://rubygems.org'

gem 'rails', '3.2.12'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'



# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem "less-rails", "~> 2.2.6"
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  gem 'therubyracer', :platforms => :ruby
  gem "sass-rails", "~> 3.2.6"
  gem "twitter-bootstrap-rails", '2.1.4'
  gem 'bootstrap-sass', '~> 2.3.0.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

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

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

gem "mongoid", "~> 3.0.0"

我在 Ubuntu 10.04.4 LTS 上,使用 rails 3.2.12,ruby 2.0.0p0(2013-02-24 修订版 39474)[i686-linux]

刚刚在我的本地环境中进行了测试。OSX 10.8.x 并出现相同的错误。

我错过了什么?

4

1 回答 1

1

我刚刚在Mongoid 主页上找到了这个:

db:create: 仅出于依赖目的而存在,实际上并没有做任何事情。

因此,当 rake 尝试访问您的数据库时(为什么这样做,我不知道),它会抛出一个错误,因为它不存在。

相反,mongoid 提供了这个:

db:seed:从 db/seeds.rb 为数据库播种

db:setup:创建索引并为数据库播种。

因此,您的基本数据库设置应在 db/seeds.rb 中定义,您可以使用

rake db:setup
于 2013-03-14T19:39:55.323 回答