当嵌入文档发生更改时,Mongoid history_track gem不会跟踪更改

时间:2012-12-04 22:04:02

标签: ruby-on-rails ruby mongoid versioning

我有以下三种模式:

class ItemGroup
    include Mongoid::Document

    embeds_many :item_attributes

    field :name
end

class ItemAttribute
    include Mongoid::Document
    include Mongoid::History::Trackable

    embedded_in :item_group

    track_history :track_create => true

    field :name
    field :min
    field :max
    field :type

    embeds_many :item_attribute_values
end

class ItemAttributeValue
    include Mongoid::Document
    include Mongoid::History::Trackable

    embedded_in :item_attribute

    track_history :on => [:name, :order], :track_create => true, :scope => :item_group_attribute, :track_delete => true

    field :name
    field :order
end

如何使用它的一个例子:

item_group: {
  name: "Televisions",
  item_attributes: [
    { name: "Screen Size", min: 14, max: 90 type: "single" },
    { name: "Screen Type", min:0, max: 0, type: "multiple", item_attribute_values: [
        { name: "LCD", order: 0 },
        { name: "LED", order: 1 },
        { name: "Plasma", order: 2 }
     ]}
  ]
}

我真正关心版本历史的是ItemAttributes。现在,如果在ItemAttribute中更改了某些内容(例如:name,min,max或type),则会创建一个新版本。但是,如果更新,删除或添加ItemAttributeValue,它将不会创建新的ItemAttribute - 这就是我们想要的。例如,如果有人将ItemAttributeValue {name:'CRT',order:3}添加到“Screen Types”,那么“Screen Types”ItemAttribute应该会获得一个新版本。版本1将具有“LCD”,“LED”,“等离子”,版本2将具有“LCD”,“LED”,“等离子”,“CRT”。如果有人删除了ItemAttributeValue,那么会创建另一个版本。

以前,ItemAttribute没有嵌入ItemGroup中,所以我能够使用Mongoid :: Versioning,它完全符合我的要求。但是,决定在ItemGroup中嵌入ItemAttribute,我不能再使用Mongoid :: Versioning了,所以我切换到了Mongoid :: History :: Tracks。这适用于跟踪模型中的更改,但是在更新模型嵌入文档时不会跟踪。

我开始认为我将不得不分叉mongoid_history来添加一个选项或能告诉gem跟踪模型嵌入文档更新的能力。

之前有人遇到这样的情况吗?

1 个答案:

答案 0 :(得分:1)

找到了解决我问题的宝石,mongoid-delorean。