Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: How to track changes like StackOverflow implements its revision history?

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.

like image 875
Andrew Avatar asked Sep 30 '10 17:09

Andrew


1 Answers

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.

like image 80
dty Avatar answered Sep 28 '22 02:09

dty