Ruby on Rails:如何跟踪更改(例如 StackOverflow 实现其修订历史记录)?
如果我要使用 Ruby on Rails 实现与 StackOverflow 问题修订历史记录相同的系统,我需要做什么才能实现这一目标?我正在创建一个像维基一样的网站,其中用户贡献的内容可以由其他人更新。我需要能够跟踪这些更改的历史记录,但我不熟悉如何实现这一点。
解决方案:
简而言之,其工作方式是创建一个额外的表来跟踪更改。表中的每一行都有记录更改之前存在的数据的“快照”(或仅更改的数据)。
有许多 Ruby Gems 已经完成了大部分工作。以下是处理版本控制/修订历史记录的宝石列表。看起来 Paper Trail 是目前最流行的这样做的宝石。 Ryan Bates 录制了一个 RailsCast,提供使用 Paper Trail 的概述。
If I were to implement a system identical to the StackOverflow question revision history using Ruby on Rails, what would I need to do in order to achieve that? I am creating a site that acts like a wiki where user contributed content can be updated by other people. I need to be able to track the history of these changes, but I am not familiar with how to implement this.
Solution:
In a nutshell, the way this works is to create an extra table to keep track of the changes. Each row in the table has a "snapshot" of the data as it existed before the record was changed (or just the data that changed).
There are a number of Ruby Gems that have already done most of the work. Here is a list of gems that deal with versioning/revision history. It looks like Paper Trail is currently the most popular gem for doing this. Ryan Bates has recorded a RailsCast providing an overview of using Paper Trail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑条目时,您不会删除(旧)条目,只需添加具有新版本号的新条目。当您想要检索要显示的条目时,您可以选择版本号最高的条目。当您想要检索条目以显示其修订历史记录时,您可以选择所有条目并按版本号对它们进行排序。
When an entry is edited, you don't delete the (old) entry, you just add a new entry with a new version number. When you want to retrieve an entry for display, you pick the one with the highest version number. When you want to retrieve an entry to show its revision history, you pick all of them and sort them by version number.
我知道这已经过时了,但任何遇到此问题的人都应该查看 Paper Trail
I know this is old but anyone who runs into this you should checkout Paper Trail