views:

193

answers:

4

Hello,

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.

+2  A: 

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.

Matt Grande
+2  A: 

I have used acts_as_versioned for stuff like this.

Sarah Mei
Your link isn't working correctly. It prompts me to go to this link instead: http://github.com/technoweenie/acts_as_versioned/tree/master
MattC
How odd - that's the exact URL in the link. If you hit return again in the address bar when you're on the 404 page...it goes to the right place. Github is doing a release today so I suspect things will settle down.
Sarah Mei
+3  A: 

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.

srboisvert
A: 

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...

Meltemi