[英]Mongoid 3 + Heroku (MongoHQ) cause Moped::Errors::OperationFailure
[英]Moped::Errors::OperationFailure when creating an embedded object
我正在將Mongoid 3.1.4與助力車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進行檢查時,可以看到在數據庫中成功創建了困擾,但是我遇到了上面提到的異常。
好的,幾天后,我得以解決此問題。
在我的窘迫模型中,我有一個before_create回調,它試圖更新Practice父對象上的字段。 這使Moped以某種方式創建了錯誤的請求,使MongoDB失敗。
我改變before_create回調after_create,現在一切工作。
希望這對其他人有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.