#177 Model Versioning
Aug 31, 2009 | 9 minutes | Active Record, Plugins
If you need to keep track of a model's history of changes and switch between versions, consider using the vestal_versions gem like I show in this episode.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
this is much nicer than acts_as_versioned! Thanks for pointing this out.
Hi Ryan,
Minor typo in your blog post, it's vestal_versions i believe, not vistal_versions.
Cheers,
Aditya
Out of curiosity, where's the wiki_link helper in your show.html.erb form?
Might I also suggest taking a look at has_versioning:
http://github.com/kkurach/has_versioning/tree/master
It doesn't only version models, it will also version associations (though not yet via has_many through) and allows for rollback.
You can even lump a whole bunch of changes into a single changelist so that you can later undo them all at once later if need be...
@Aditya, fixed, thanks!
@August, the wiki_link helper method is inside the application_helper.rb file. Check out the full source code link in the show notes.
@Shane, thanks for pointing this out! I hadn't heard of has_versioning.
I'm wondering how to get this to work with associated models, so if the associations change this is reflected in the versioned model.
I could implement a simple after_update method that touches the parent (i.e. versioned) model when the associated model is updated but I'm wondering will the parent model recognise the changes. Particularly as the main aspect that will change on the associated model is the attached file via paperclip!
If anyone wants to do a "diff" output with this to compare versions, check out HTMLDIff
http://github.com/myobie/htmldiff/tree/master
It will put new stuff in green, old in red with strikethrough, etc.
I setup something like this with the acts_as_audited plugin a while back which is also good, looks very similar.
This looks good.
You may also like my PaperTrail gem, which doesn't require you to touch existing records to store their starting state. It also lets you recover destroyed records, and copes with intermediate schema changes. It also records who was responsible.
Forgot the link:
http://github.com/airblade/paper_trail/tree/master
@Shane
has_versioning plugin ( http://github.com/kkurach/has_versioning/tree/master ) doesn't support rollbacks yet. everything else you wrote is correct :)
it supports tracking objects AND associations, and also packing many changes into one changelist
Anyone wondering about the name of the vestal_versions plugin probably needs to listen to A Whiter Shade of Pale
http://www.youtube.com/watch?v=PbWULu5_nXI
works great but one problem - my model has a "state" column that gets changed from "pending" to "active". this would allow users to revert an active post to pending - is there a way to prevent Vestal Versions from not tracking a specific column (in this case the "state" column)? thanks!
Hi, very nice screencast
can someone explain me what the & in
Page.find_each(&:touche)
does?
thx
@sam
Event shorter: Ryan made an episode about that: http://railscasts.com/episodes/6-shortcut-blocks-with-symbol-to-proc
In short: its just a shortcut for
Page.find_each{|item| item.touche}
The & converts the symbol to a proc object ( to be more specific the & calls the to_proc method on the symbol or on any other object), which gets then called for each object which find_each returns.
Thats feature is in ruby 1.9 per default, or you can define it like rails did.
Look at http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthand and search for to_proc
I've been using acts_as_revisable on my project, which has worked out extremely well, it seems to be a bit more developed, and works very will in conjunction with AASM. acts_as_revisable also supports association versioning as detailed in the without_scope blog: http://withoutscope.com/2009/5/12/simple-association-versioning-with-acts_as_revisable
http://github.com/rich/acts_as_revisable/tree/master
What version of Rails does vestal_versions require, will it work on 2.1?
For getting the log and the hirb working, install the hirb gem and put
if ENV['RAILS_ENV']
require 'rubygems'
require 'hirb'
require 'activerecord'
Hirb.enable
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
into .irbrc and then fire up script/console
I was using acts_as_versioned. Thanks Ryan. This one is good.
@Bryan, Thanks. I got the HTMLDiff working as well. Its fantastic.
Hi,
I've been using acts_as_versioned with acts_as_paranoid, which need(ed) a custom patch to work proprely together in a project (the patch ensured that deleting a record not only sets the deleted_at column to the current date-time, but also moves it to the *_versions table and was provided on the versioned act track or somewhere like that; btw the patch on the site didn't work but I mailed the author and he gave me the correct one - obviuosly he didn't upload the patch right but was very helpful).
Since Rails 2.1.x came out with the enhanced change-tracking functionalities and acts_as_versioned stopped working, I'm stuck using Rails 2.0.2 for that project.
So, my question is: Is there a way/act/plugin to achieve what I now have with newer Rails versions?
The idea is that I need to keep track of both changes and deletions
I ended up creating a simple fork of vestal_versions so that I could specify which columns to include or exclude. Details are at http://github.com/sthapit/vestal_versions
In Rails 2.3.4 it says: undefined method `password_confirmation' for #<User:0x7f4ffda0a7e0>
I added an
attr_accessor :password_confirmation
to the User-Model
But still got the Error:
Password confirmation is too short (minimum is 4 characters)
There is newer version of vestal_versioned.
Some changes:
To install it add:
config.gem 'vestal_versions'
And there is no need for adding migration to get version 1 for existing records.
Ryan,
Thanks so much for cool casts !!
Btw, I have a little problem implementing vestal_versions as followed from this episode.
It'd be so kind if you'd have some solutions to this
http://railsforum.com/viewtopic.php?pid=132945#p132945
I have an issue with this when im deploying my app to heroku, the vestal_versions gem works great locally but not on heroku. It seems that the versions are not working and my records are not being updated when I edit a post.
Anyone know whats going on?
Thanks, Vincent.