我正在尝试向 PaperTrail::Version 添加验证,这将防止敏感数据存储在版本表中。这个想法是,如果您忘记清理模型中的 has_paper_trail 调用,就会出现很多明显的错误。
如果我在 config/initializers/paper_trail 中添加一个自定义验证器,它可以工作……一段时间。然后 PaperTrail 开始使用其默认行为,我的方法未定义。
示例代码:
PaperTrail::Rails::Engine.eager_load!
module PaperTrail
class Version
# Ensure no sensitive values end up in the versions table
validate :prohibited_attributes
...
最佳答案
尝试自定义版本类。请参阅文档 section 6.a. Custom Version Classes .
6.a. Custom Version Classes
You can specify custom version subclasses with the
:class_name
option:class PostVersion < PaperTrail::Version # custom behaviour, e.g: self.table_name = :post_versions end class Post < ActiveRecord::Base has_paper_trail :class_name => 'PostVersion' end
使用 PaperTrail::Rails::Engine.eager_load!
是个好主意。不知道为什么这对你不起作用。希望这是一种解决方法。
关于ruby - 将 ActiveRecord 验证添加到 PaperTrail 的版本模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41104520/