本文介绍了Rails 中的对象版本控制,如 Papertrail,但单独的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我目前正在处理的项目,我需要实施对象版本控制.不幸的是,我需要保留每个对象的完整历史记录,因此像 Papertrail 这样的单表解决方案很快就会变得无法管理.但是,我喜欢 Papertrail 的一些功能,但我无法在为每个模型(例如 act_as_versioned)提供单独表的解决方案中找到这些功能.

For a project I'm currently working on I need to implement object versioning. Unfortunately I need to keep a full history of each object, so a single table solution like Papertrail would quickly become un-manageable. There are features of Papertrail which I like, however, which I haven't been able to find in a solution with individual tables for each model (such as acts_as_versioned).

  • 能够存储来自控制器和模型的元信息
  • 数据已序列化,因此架构更改不会修改版本表
  • 强大的版本遍历方法
  • 自动跟踪变更责任

还有一些 Papertrail 没有的功能可以作为奖励:

There are also some features which Papertrail does not have which would be bonuses:

  • 内置版本差异支持
  • 不同版本而非完整版本

我目前正在考虑分叉 Papertrail 以便为每个模型使用单独的表,但如果有现有的解决方案,我希望节省这项工作.

I am currently considering forking Papertrail to use individual tables for each model, but would like to save that effort if there's an existing solution.

更新:默认情况下,Vestal Versions 使用单个表,但是通过为每个模型提供自定义版本类并使用 ActiveRecord 的set_table_name"方法,我能够为每个模型创建单独的表.Vestal Versions 还内置了 diff 支持,尽管它的界面不如 Papertrails 强大.它还缺乏关联支持.

Update:Vestal Versions by default uses a single table, but by providing a custom version class for each model and using the "set_table_name" method of ActiveRecord, I was able to create separate tables for each model. Vestal Versions also has built in diff support, though its interface isn't as powerful as Papertrails. It also lacks association support.

更新 2:由于 papertrail 似乎是一个更活跃的项目,我已经分叉了 gem 并添加了类似于 vestal 版本的自定义类支持,现在允许为每个模型定义单独的表.我的 fork 在这里,但我希望它很快就会被拉入主项目存储库.https://github.com/benzittlau/paper_trail

Update 2:As papertrail seems to be a more active project I've forked the gem and added in custom class support similar to vestal versions which now allows for the ability to define separate tables per model. My fork is here, but I hope it will be pulled into the main project repository shortly.https://github.com/benzittlau/paper_trail

推荐答案

您可以使用 :class_name 选项指定自定义版本子类:

You can specify custom version subclasses with the :class_name option:

class Post < ActiveRecord::Base
  has_paper_trail :class_name => 'PostVersion'
end

class PostVersion < Version
  # custom behaviour, e.g:
  self.table_name = :post_versions # in rails 2, use set_table_name
end

这允许您将每个模型的版本存储在单独的表中,如果您要创建很多版本,这将非常有用.

This allows you to store each model's versions in a separate table, which is useful if you have a lot of versions being created.

这篇关于Rails 中的对象版本控制,如 Papertrail,但单独的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 01:28