Rails - 生产中的Mongoid,rake命令错误:

时间:2013-03-14 17:08:17

标签: ruby-on-rails ruby mongodb mongoid rake

我的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

的Gemfile:

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出现相同的错误。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我刚在Mongoid home page上找到了这个:

  

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

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

相反,mongoid提供了这个:

  

db:seed:从db / seeds.rb

中种植数据库      

db:setup:创建数据库的索引和种子。

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

加载它。
rake db:setup