kyku
kyku

Reputation: 6042

Techniques for storing/retrieving historical data in Rails

I'm looking for some ideas about saving a snapshot of some (different) records at the time of an event, for example user getting a document from my application, so that this document can be regenerated later. What strategies do you reccomend? Should I use the same table as the table with current values or use a historical table? Do you know of any plugins that could help me with the task? Please share your thoughts and solutions.

Upvotes: 1

Views: 1136

Answers (4)

Meltemi
Meltemi

Reputation: 38359

The OP is a year old but thought I'd add vestal_versions to the mix. It uses a single table to track serialized hashes of each version. By traversing the record of changes, the models can be reverted to any point in time.

Seems to be the community favorite as of this post...

Upvotes: 0

srboisvert
srboisvert

Reputation: 12749

There are several plugins for this.

Acts_audited

acts as audited creates a single table for all of the auditable objects and requires no changes to your existing tables. You get on entry per change with the changes stored as a hash in a memo field, the type of change (CRUD). It is very easy to setup with just a single statement in the application controller specifying which models you want audited. Rolling back is up to you but the information is there. Because the information stored is just the changes building the whole object may be difficult due to subsequent changes.

Acts_as_versioned

A bit more complicated to setup- you need a separate table for each object you want to version and you have to add a version id to your existing table. Rollback is a very easy. There are forks on github that provide a hash of changes since last version so you can easily highlight the differences (it's what I use). My guess is that this is the most popular solution.

Ones I have no experience with: acts_as_revisable. I'll probably give this one a go next time I need versioning as it looks much more sophisticated.

Upvotes: 3

Sarah Mei
Sarah Mei

Reputation: 18484

I have used acts_as_versioned for stuff like this.

Upvotes: 2

Matt Grande
Matt Grande

Reputation: 12157

I did this once awhile back. We created a new table that had a very similar structure to the table we wanted to log and whenever we needed to log something, we did something similar to this:

attr = object_to_log.attributes
# Remove things like created_at, updated_at, other unneeded columns
log = MyLogger.new(attrs)
log.save

There's a very good chance there are plugins/gems to do stuff like this, though.

Upvotes: 2

Related Questions