【问题标题】:Moped::Errors::OperationFailure when creating an embedded object创建嵌入对象时的 Moped::Errors::OperationFailure
【发布时间】:2014-08-18 19:10:45
【问题描述】:

我将 mongoid 3.1.4 与 moped 1.5.1、mongodb 2.4.1 和 ruby​​ 1.9.3 一起使用。

我有下一个模型:

class Practice
  include Mongoid::Document
  embeds_many :distresses
end

class Distress
  include Mongoid::Document
  embedded_in :practice
end

当我做这样的事情时,它似乎正在工作:

practice = Practice.create
practice.distresses.create

但是当我将 safe: true 放在我的配置文件中并执行相同操作时,我会得到:

Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command
  @length=82
  @request_id=22
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name="collection.$cmd"
  @skip=0
  @limit=-1
  @selector={:getlasterror=>1, :safe=>true}
  @fields=nil>

实际上,我在以任何方式制造痛苦时都遇到了错误。这也会引发异常:

practice = Practice.create
distress = practice.distresses.build
distress.save

当我检查 practice.distresses.count 时,我可以看到在数据库中成功创建了苦恼,但是我得到了上面提到的异常。

【问题讨论】:

  • 请给我看你的控制器代码,用于新建和创建!!!

标签: mongodb mongoid3 moped


【解决方案1】:

好的,几天后我解决了这个问题。

在我的 Distress 模型中,我有一个 before_create 回调,它试图更新 Practice 父对象上的一个字段。不知何故,这使得 Moped 创建了一个错误的请求,导致 MongoDB 失败。

我为 after_create 更改了 before_create 回调,现在一切正常。

希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多