[英]Modify or Create paper_trail version with alternate attribute value
I have a model with a start_date and end_date.我有一个带有 start_date 和 end_date 的 model。
When a version is created, if the old end_date is after the new start_date then the end_date in the paper_trail version should be set to start_date of the updated record创建版本时,如果旧的 end_date 在新的 start_date 之后,则 paper_trail 版本中的 end_date 应设置为更新记录的 start_date
In sudo code this might look like...在 sudo 代码中,这可能看起来像......
(doesn't work and I'd prefer to not modify the version after creating it) (不起作用,我不想在创建版本后修改它)
record.save
version = paper_trail.previous_version
if (version.end_date > record.start_date)
version.end_date = record.start_date
version.save
end
The sequence would look like this...序列看起来像这样......
Create record创建记录
record: start_date=2021, end_date=nil
Update record更新记录
record: start_date=2022, end_date=2023
version1: start_date=2021, end_date=2022
Update record更新记录
record: start_date=2024, end_date=2037
version2: start_date=2022, end_date=2024
version1: start_date=2021, end_date=2022
rails 6.1, paper_trail 12轨道 6.1,paper_trail 12
I would suggest keeping the Papertrail behavior as-is, meaning it would keep track of value changes for each field without any additional custom logic that would overwrite the values, thus breaking interface for rolling back to a version etc.我建议保持 Papertrail 行为原样,这意味着它将跟踪每个字段的值更改,而无需任何额外的自定义逻辑来覆盖这些值,从而破坏回滚到版本等的接口。
Instead, I would use meta
to store and retrieve any custom data you need for the model https://github.com/paper-trail-gem/paper_trail#4c-storing-metadata相反,我会使用
meta
来存储和检索 model https://github.com/paper-trail-gem/paper_trail#4c-storing-metadata所需的任何自定义数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.