Object versioning in Rails, like Papertrail but individual tables

前端 未结 2 941
不思量自难忘°
不思量自难忘° 2021-02-05 15:04

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 Paper

相关标签:
2条回答
  • 2021-02-05 15:22

    I've been using Vestal Versions, a rails gem/plugin that uses the memento pattern to create a history table, and it saves a diff of attributes in after_save and after_destroy callbacks.

    It also records when it was changed and increments a version number so you can rollback to a certain version or a version present at a certain date or time.

    I can then pull up an object like so:

    @user = User.last
    @user.versions.last.changes #=> {:name => ['Robert','Bob']}
    

    I'm a pretty big fan.

    0 讨论(0)
  • 2021-02-05 15:30

    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.

    0 讨论(0)
提交回复
热议问题