[英]Create paper_trail New Version without updating existing Record
I've integrated paper_trail with a Project Model.我已经将 paper_trail 与项目 Model 集成在一起。
Normal Flow:-正常流量:-
Whenever project Record in model gets updated, a new version with changes gets created in Versions table.每当 model 中的项目记录更新时,都会在版本表中创建具有更改的新版本。
Requirement:-要求:-
Whenever any project gets updated, rather than updating project record itself in projects model, i want to create a new Version Record.每当任何项目更新时,我都想创建一个新的版本记录,而不是更新项目 model 中的项目记录本身。 is there any way to do it?
有什么办法吗?
I'm using Rails 5.1.6.2, paper_trail (10.3.0)我正在使用 Rails 5.1.6.2,paper_trail (10.3.0)
I'd try using PaperTrail::Events::Update
, though it's not public API (it may change at any time without warning)我会尝试使用
PaperTrail::Events::Update
,尽管它不是公开的 API (它可能随时更改而不会发出警告)
project.name = 'new name'
update = PaperTrail::Events::Update.new(project, ....)
PaperTrail::Version.create!(update.data)
Again, this is not public API, so you will void your warranty:)同样,这不是公开的 API,因此您的保修将失效:)
Whenever any project gets updated, ..
每当任何项目得到更新,..
I'd put this in my ProjectController#update
action only.我只会把它放在我的
ProjectController#update
动作中。 You could use a model callback like before_save
if you really have to, and throw:abort
to halt the callback chain, but you'll have to work around the implicit transaction.如果你真的需要的话,你可以使用像
before_save
这样的 model 回调,并且throw:abort
来停止回调链,但是你必须解决隐式事务。
We also have this flow in our app where a user can edit a record, but the edits have to be approved or rejected before going live.我们的应用程序中也有此流程,用户可以在其中编辑记录,但必须在上线前批准或拒绝编辑。 So we use PaperTrail as intended, but then immediately rollback the changes and created a workflow for the approval/rejection that reinstates them or not.
因此,我们按预期使用 PaperTrail,但随后立即回滚更改并创建了一个用于批准/拒绝恢复或不恢复它们的工作流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.