我正在开发一个 Rails 应用程序,我需要在其中显示审核跟踪Record,其中 has_manyData。我的记录上有 paper_trail 和关联的基准模型,并且它可以很好地保存它们的版本。
然而,我需要的是one每当创建记录的版本一个或多个相关数据已更改。目前,它会在每个发生变化的数据上创建版本,但仅当记录的属性发生变化时才会创建记录的版本;当关联的数据更改时它不会执行此操作。
我尝试将 touch_with_version 放入 Record 的 after_touch 回调中,如下所示:
class Record < ActiveRecord::Base
has_many :data
has_paper_trail
after_touch do |record|
puts 'touched record'
record.touch_with_version
end
end
and
class Datum < ActiveRecord::Base
belongs_to :record, :touch => true
has_paper_trail
end
after_touch 回调会触发,但不幸的是它为每个 Datum 创建一个新版本,因此当创建一条 Record 时,它已经有大约 10 个版本,每个 Datum 一个版本。
有没有办法在回调中判断版本是否已创建,这样我就不会创建多个版本?就像签入 Record 回调之一,如果 Datum 已经触发了一个版本,是否不再执行任何操作?
Thanks!
这对我有用。
class Place < ActiveRecord::Base
has_paper_trail
before_update :check_update
def check_update
return if changed_notably?
tracking_has_many_associations = [ ... ]
tracking_has_has_one_associations = [ ... ]
tracking_has_many_associations.each do |a|
send(a).each do |r|
if r.send(:changed_notably?) || r.marked_for_destruction?
self.touch
return
end
end
end
tracking_has_one_associations.each do |a|
r = send(a)
if r.send(:changed_notably?) || r.marked_for_destruction?
self.touch
return
end
end
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)