加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 查询上的Mongoid无限循环

发布时间:2020-12-17 02:55:48 所属栏目:百科 来源:网络整理
导读:我在heroku实例上使用mongolab和monogid. 我可以在rails控制台中访问我的模型并创建它们. 但是 – 调用Model.first / Model.find_or_initialize_by会导致无限循环 MOPED: xx:29454 COMMAND database=admin command={:ismaster=1} runtime: 7.8211ms MOPED: x
我在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 Sandbox都是3.0版),你必须使用Mongoid 5.x.”.我已经运行了一个包更新,所以我没有想太多.

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

gem ‘mongoid’,‘~> 5.0’

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

rails g mongoid:config

mongoid.yml将其嵌套更改为列出“客户端”而不是“会话”,这实际上会混淆Rails.

这就是为我解决的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读