【问题标题】:restore main Object to a Specific Version by using Paper Trail使用 Paper Trail 将主对象恢复到特定版本
【发布时间】:2019-10-02 08:19:27
【问题描述】:

如何将特定版本中的更改应用到主对象?

例如:

假设有一个项目模型。每当更新项目记录时,系统都会创建一个版本。现在我有一个要求,用户可以将此项目记录移动到任何特定版本并 更新在该版本期间更改的所有属性。

 Project 1 has following Versions:
  V5 
  V4
  V3
  V2
  V1

 if user wants he can move Project to any of these 5 Versions and apply the changes 
 made in that version in project record.

我正在使用

ruby 2.5.3、Rails 5.1.6.2 和 paper_trail (10.3.0)

【问题讨论】:

    标签: ruby-on-rails ruby rubygems ruby-on-rails-5 paper-trail-gem


    【解决方案1】:

    文档说每个版本还有一个 created_at 属性。所以你可以在给定的时间找到版本:

    widget = widget.paper_trail.version_at(1.day.ago)  # the widget as it was one day ago
    widget.save                                        # reverted
    

    如果您知道用户想要什么版本,那么您也知道 created_at 以便您可以将其设置为正确的版本。

    wanted_version = # Find the correct version that the user wants
    widget = widget.paper_trail.version_at(wanted_version.created_at)
    widget.save
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      相关资源
      最近更新 更多