我有一些模型,其中父模型需要为使缓存摘要无效而触及 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
在这种情况下正确使用什么?当只需要启动层次结构时,我是否需要使用这些回调?文档不清楚这应该如何运作。任何人都可以为我澄清这一点吗?谢谢。