0

我有一些模型,其中父模型需要为使缓存摘要无效而触及 updated_at 字段。这是我的基本(简化)模型:

    class Location
        include Mongoid::Document
        include Mongoid::Timestamps
        has_many :departments

        field :name, type: String
    end

    class Department
        include Mongoid::Document
        include Mongoid::Timestamps
        belongs_to :location, touch: true
        has_many :positions

        field :name, type: String
    end

    class Position
        include Mongoid::Document
        include Mongoid::Timestamps
        belongs_to :department, touch: true

        field :name, type: String
    end

如果我创建一个新职位,则会触及 Department 和 Location 祖先。不幸的是,如果我更新或删除职位,什么都不会发生。我已经看到需要手动调用 touch 的引用,例如:

    after_save :touch
    before_destroy :touch

在这种情况下正确使用什么?当只需要启动层次结构时,我是否需要使用这些回调?文档不清楚这应该如何运作。任何人都可以为我澄清这一点吗?谢谢。

4

1 回答 1

0

我自己也遇到了这个问题。根据 after_save/before_save 解决方案,您可能已经遇到过这个 GitHub 问题,但它的要点是触摸选项按预期工作。也就是说,它仅在创建或销毁子文档时触发(有一个奇怪的销毁情况也没有完全覆盖)。

https://github.com/mongoid/mongoid/issues/2373

于 2013-10-05T00:48:20.083 回答