获取对嵌入式mongoid对象的引用

时间:2017-07-03 19:50:22

标签: ruby-on-rails mongodb mongoid

我有一个带有嵌入式项目列表的mongoid模型

Class Menu
  include Mongoid::Document

  field :name
  field :code

  has_many :items
end

Class Item
  field :name
end

我试图在保留对类变量

中项目的引用的同时附加到model.items

然而我似乎得到了一个克隆呢?

我该如何完成这个

    13: def run(model)
    14:   model.items << object
    15:   binding.pry
 => 16:   @object = model.order_items.last
    17:   model
    18: end

> object.id
=> BSON::ObjectId('595a912b8f337c6da0d764db')
> model.order_items.first.id
=> BSON::ObjectId('595a91268f337c6da0d764da')
> foo = model.order_items.last
> foo.id
=> BSON::ObjectId('595a91268f337c6da0d764da')    <-- ok expected
> @object = foo
> object.id
=> BSON::ObjectId('595a91718f337c6da0d764dc')   <--wtf?
> @object.id
=> BSON::ObjectId('595a91718f337c6da0d764dc')   <-- double wtf?
Add Commententer code here

0 个答案:

没有答案