Sco*_*ott 4 ruby-on-rails paper-trail-gem ruby-on-rails-5
我们刚刚开始使用PaperTrail gem,并注意到版本表中 75% 的记录列为零object_changes。知道为什么会发生这种情况以及我们如何阻止它吗?
使用 Rails 5.1 和 PaperTrail 10.1。
根据@Scott 的回答,创建一个初始值设定项并设置 PaperTrail 的全局配置(仅限版本 10+)以忽略:touch事件。
这在我们的数据库中创建了数百万个不必要的版本。
config/initializers/paper_trail.rb
PaperTrail.config.has_paper_trail_defaults = {
on: %i[create update destroy]
}
Run Code Online (Sandbox Code Playgroud)
零对象更改是由于跳过的属性上的触摸事件造成的。我为此提出的唯一解决方案是仅跟踪创建、更新和销毁的版本。
我还发现我们有重复的版本记录。我们通过将以下内容放入 中来为所有模型打开 PaperTrail ApplicationRecord,如果一个类继承于另一个类,这会导致创建重复版本。即如果您有class Foo < Bar并且这样做Bar.create将创建 2 个相同的版本记录。
初始版本于ApplicationRecord
def self.inherited(subclass)
super
subclass.send(:has_paper_trail)
end
Run Code Online (Sandbox Code Playgroud)
最终版本
def self.inherited(subclass)
classes_to_skip = %w[Foo]
attributes_to_skip = [:bar_at]
on_actions = [:create, :update, :destroy]
super
unless classes_to_skip.include?(subclass.name)
subclass.send(:has_paper_trail, on: on_actions, ignore: attributes_to_skip)
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
990 次 |
| 最近记录: |