【发布时间】:2015-10-28 14:51:05
【问题描述】:
我正在运行 Ruby 2.1 和 Mongoid 5.0(没有 Rails)。
我想跟踪 before_save 回调是否嵌入字段已更改。
我可以使用document.attribute_changed? 或document.changed 方法检查普通字段,但不知何故,这些方法不适用于关系(embed_one、has_one 等)。
有没有办法在保存文档之前检测这些更改?
我的模型是这样的
class Company
include Mongoid::Document
include Mongoid::Attributes::Dynamic
field :name, type: String
#...
embeds_one :address, class_name: 'Address', inverse_of: :address
#...
before_save :activate_flags
def activate_flags
if self.changes.include? 'address'
#self.changes never includes "address"
end
if self.address_changed?
#This throws an exception
end
end
我如何保存文档的一个例子是:
#...
company.address = AddressUtilities.parse address
company.save
#After this, the callback is triggered, but self.changes is empty...
#...
我已经阅读了文档并谷歌了它,但我找不到解决方案?
我找到了this gem,但它很旧,并且不适用于较新版本的 Mongoid。在考虑尝试修复/请求 gem 之前,我想检查是否有其他方法...
【问题讨论】: