ruby-on-rails - Mongoid 无限循环查询

标签 ruby-on-rails ruby heroku mongoid mlab

我在 heroku 实例上使用 mongolab 和 monogid。

我可以在 Rails 控制台中访问我的模型并创建它们。

但是 - 调用 Model.first/Model.find_or_initialize_by 会导致无限循环

MOPED: xx:29454 COMMAND      database=admin command={:ismaster=>1} runtime: 7.8211ms
  MOPED: xx:29454 QUERY        database=heroku_zl6cgccm 

collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1.6873ms
MOPED: xx:29454 QUERY        database=heroku_xx collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 5.3479ms
  MOPED: xx:29454 QUERY        database=heroku_xx collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 8.8738ms

一直到:

collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1544.7719ms
SystemStackError: stack level too deep
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/string.rb:79:in `to_bson_cstring'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/string.rb:63:in `to_bson_key'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/symbol.rb:58:in `to_bson_key'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:45:in `block (2 levels) in to_bson'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:43:in `each'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:43:in `block in to_bson'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/encodable.rb:57:in `encode_with_placeholder_and_null'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:42:in `to_bson'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/protocol/message.rb:173:in `serialize_selector'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/protocol/message.rb:325:in `serialize'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/connection.rb:171:in `block in write'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/connection.rb:169:in `each'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/connection.rb:169:in `write'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/authenticatable.rb:64:in `login'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/operation/read.rb:55:in `block (2 levels) in execute'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/node.rb:118:in `block in connection'
... 9285 levels...
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/query_cache.rb:190:in `first_with_cache'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:199:in `block (2 levels) in first'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:545:in `with_sorting'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:198:in `block in first'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:457:in `try_cache'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:197:in `first'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual.rb:20:in `first'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/findable.rb:144:in `first'
    from (irb):10
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
    from /app/bin/rails:8:in `require'

这是我的 mongoid.yml 文件:

production:
 sessions:
   default:
     uri: <%= ENV['MONGOLAB_URI'] %>

     options:

       max_retries: 30
       retry_interval: 1
       timeout: 15

       refresh_interval: 10

最佳答案

我在使用 MongoLab 时遇到了类似的问题,在我达到堆栈级别太深之前会进行无限的数据库查询。我的客户想要一个免费的数据库,所以我花了整整 12 个小时进行故障排除。

我尝试了上面的一切,以及一些其他的配置选项,然后偶然发现了这个: https://devcenter.heroku.com/articles/mongolab

其中有一行我忽略了:“如果您使用的是 MongoDB 3.0 版(所有 MongoLab 沙箱都是 3.0 版),则必须使用 Mongoid 5.x。”。我已经运行了捆绑更新,所以我没有多想。

您应该进入您的 gemfile 并指定:

gem 'mongoid', '~> 5.0'

我还必须删除我的 mongoid.yml 并重新开始

rails g mongoid:config

mongoid.yml 将其嵌套更改为列出“客户端”而不是“ session ”,这确实让 Rails 感到困惑。

这就是为我修复它的原因。

关于ruby-on-rails - Mongoid 无限循环查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32951455/

相关文章:

ruby-on-rails - 使用 expectPOST 测试 AngularJS Controller

ruby - 如何在 Ruby 中为 String 类创建新函数?

heroku - 云端的 Node.js TCP 套接字服务器 [Heroku/AppFog]

javascript - 排除 Rails 部署的 Heroku ExecJS 程序错误

javascript - 在javascript中将rails变量传递给ajax

javascript - 为何今天如此插入客户端框架?

javascript - 使用 selectize.js 在下拉列表中仅显示三个输入

Ruby Exercise 18 undefined method 虽然我定义了方法

ruby-on-rails - 泛型示例问题

heroku - Heroku 与 Postgres 上的 ConnectionTimeoutError