0

My models are:

class Node
  include Mongoid::Document
end

class PhysicalServer < Node
  embeds_many :network_interfaces
end

class NetworkInterface
  include Mongoid::Document
  embedded_in :physical_server
end

If I do:

server.network_interfaces.build()
server.save!

when I check database, I will see 2 NetworkInterface embedded documents with duplicate ids.

However if I do:

server.network_interfaces.create()

it'll work correctly (only 1 embedded document created).

Is it a bug in Mongoid, or there is something wrong with my code? I'm using Ruby1.9.3 + Rails 3.2.9 + Mongoid 3.0.13

4

1 回答 1

0

不太确定“还”是什么问题,但我遇到了同样的问题,我暂时有一个解决方法。

通过在控制器的更新操作中执行强制新查找,我能够摆脱在每次调用更新时创建重复的“$pushAll”。我感觉这与 Mongoid 建立原子操作有关;使用新对象只是删除“构建”操作。

我已经创建了问题的要点(希望能够重新创建失败的场景和解决方法:https ://gist.github.com/jsmestad/d0103ba0197df9f4505b )

于 2013-02-22T23:33:42.703 回答