NoMethodError:用户创建轨道上nil:NilClass的未定义方法“ each”

时间:2018-09-24 03:46:59

标签: ruby-on-rails heroku mongoid

当我在开发中运行Rails 5.1应用程序时,一切都很好,但是当我在Heroku上运行它并尝试创建用户时,我总是遇到此错误。

    NoMethodError: undefined method `each' for nil:NilClass
/app/vendor/bundle/ruby/2.4.0/gems/mongo-2.5.3/lib/mongo/cluster.rb:192:in `initialize'
/app/vendor/bundle/ruby/2.4.0/gems/mongo-2.5.3/lib/mongo/client.rb:454:in `new'
/app/vendor/bundle/ruby/2.4.0/gems/mongo-2.5.3/lib/mongo/client.rb:454:in `create_from_addresses'
/app/vendor/bundle/ruby/2.4.0/gems/mongo-2.5.3/lib/mongo/client.rb:253:in `initialize'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/clients/factory.rb:64:in `new'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/clients/factory.rb:64:in `create_client'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/clients/factory.rb:27:in `create'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/clients.rb:69:in `block in with_name'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/clients.rb:68:in `synchronize'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/clients.rb:68:in `with_name'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/persistence_context.rb:110:in `client'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/persistence_context.rb:70:in `collection'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/clients/options.rb:73:in `collection'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/contextual/mongo.rb:342:in `initialize'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/contextual.rb:53:in `new'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/contextual.rb:53:in `create_context'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/contextual.rb:35:in `context'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/contextual.rb:20:in `exists?'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/validatable/uniqueness.rb:276:in `validate_root'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/validatable/uniqueness.rb:48:in `block in validate_each'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/validatable/queryable.rb:23:in `with_query'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/validatable/uniqueness.rb:42:in `validate_each'
/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.1.0/lib/active_model/validator.rb:150:in `block in validate'
/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.1.0/lib/active_model/validator.rb:147:in `each'
/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.1.0/lib/active_model/validator.rb:147:in `validate'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:413:in `block in make_lambda'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:178:in `block (2 levels) in halting_and_conditional'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:601:in `block (2 levels) in default_terminator'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:600:in `catch'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:600:in `block in default_terminator'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:179:in `block in halting_and_conditional'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:507:in `block in invoke_before'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:507:in `each'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:507:in `invoke_before'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:130:in `run_callbacks'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/interceptable.rb:132:in `run_callbacks'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:825:in `_run_validate_callbacks'
/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.1.0/lib/active_model/validations.rb:405:in `run_validations!'
/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.1.0/lib/active_model/validations/callbacks.rb:110:in `block in run_validations!'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:131:in `run_callbacks'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/interceptable.rb:132:in `run_callbacks'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/callbacks.rb:825:in `_run_validation_callbacks'
/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.1.0/lib/active_model/validations/callbacks.rb:110:in `run_validations!'
/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.1.0/lib/active_model/validations.rb:335:in `valid?'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/validatable.rb:97:in `valid?'
/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.1.0/lib/active_model/validations.rb:372:in `invalid?'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/persistable/creatable.rb:117:in `prepare_insert'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/persistable/creatable.rb:23:in `insert'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/persistable/savable.rb:23:in `save'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/persistable/savable.rb:44:in `save!'
/app/db/seeds.rb:15:in `block in <top (required)>'
/app/db/seeds.rb:4:in `times'
/app/db/seeds.rb:4:in `<top (required)>'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `block in load'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
/app/vendor/bundle/ruby/2.4.0/gems/mongoid-7.0.1/lib/mongoid/railties/database.rake:20:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/cli/exec.rb:74:in `load'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/cli/exec.rb:74:in `kernel_load'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/cli/exec.rb:27:in `run'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/cli.rb:360:in `exec'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor.rb:369:in `dispatch'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/cli.rb:20:in `dispatch'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor/base.rb:444:in `start'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/cli.rb:10:in `start'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/exe/bundle:30:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
/app/vendor/bundle/ruby/2.4.0/gems/bundler-1.15.2/exe/bundle:22:in `<top (required)>'
/app/bin/bundle:3:in `load'
/app/bin/bundle:3:in `<main>'

我无法放置可能导致错误的原因。它在mlab中的mongoid上运行数据库。不知道这是否是原因。我无法在heroku控制台中为数据库设置种子,而不会出现相同的错误。

1 个答案:

答案 0 :(得分:2)

Mongo驱动程序似乎希望在多个主机URI上进行迭代,但是在某种程度上设法在代码中达到了这一点,而未检测到未定义任何内容。

当您使用Mongoid与(较低级别的)Mongo Ruby客户端进行交互时,此file on configuration可能会为您提供一些线索。您可能还希望参考在Heroku上安装的任何MongoDB插件的文档。