#177
Aug 31, 2009

Model Versioning

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 (14.4 MB, 9:28)
alternative download for iPod & Apple TV (10.4 MB, 9:28)

Resources

sudo rake gems:install
script/generate vestal_versions_migration
script/generate migration version_existing_pages
rake db:migrate
# config/environment.rb
config.gem 'laserlemon-vestal_versions', :lib => 'vestal_versions', :source => 'http://gems.github.com'

# models/page.rb
class Page < ActiveRecord::Base
  versioned
end

# version_existing_pages migration
say_with_time "Setting initial version for pages" do
  Page.find_each(&:touch)
end

# pages_controller.rb
def show
  @page = Page.find(params[:id])
  @page.revert_to(params[:version].to_i) if params[:version]
end

# script/console
p = Page.all
p.versions
p.revert_to(7.minutes.ago)
p.content
p.revert_to(:last)
<p>
  <%= link_to "Edit", edit_page_path(@page) %>
  | Version <%= @page.version %>
  <% if @page.version > 1 %>
    | <%= link_to "Previous version", :version => @page.version-1 %>
  <% end %>
  <% if params[:version] %>
    | <%= link_to "Latest version", :version => nil %>
  <% end %>
</p>

RSS Feed for Episode Comments 53 comments

Episode #177: Model Versioning

1. Torsten Aug 31, 2009 at 00:23

this is much nicer than acts_as_versioned! Thanks for pointing this out.


Episode #177: Model Versioning

2. Aditya Sanghi Aug 31, 2009 at 01:25

Hi Ryan,
Minor typo in your blog post, it's vestal_versions i believe, not vistal_versions.

Cheers,
Aditya


Episode #177: Model Versioning

3. August Lilleaas Aug 31, 2009 at 03:42

Out of curiosity, where's the wiki_link helper in your show.html.erb form?


Episode #177: Model Versioning

4. Tobias Aug 31, 2009 at 06:21

Thanks a lot thats just what I was locking for for our next sprint next week :-).


Episode #177: Model Versioning

5. Shane Aug 31, 2009 at 06:55

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


Episode #177: Model Versioning

6. Ryan Bates Aug 31, 2009 at 07:25

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


Episode #177: Model Versioning

7. Jon Aug 31, 2009 at 07:43

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!


Episode #177: Model Versioning

8. Brian Armstrong Aug 31, 2009 at 07:47

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.


Episode #177: Model Versioning

9. Andy Stewart Aug 31, 2009 at 09:15

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.


Episode #177: Model Versioning

10. Andy Stewart Aug 31, 2009 at 09:15

Forgot the link:

http://github.com/airblade/paper_trail/tree/master


Episode #177: Model Versioning

11. Karol Aug 31, 2009 at 09:56

@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


Episode #177: Model Versioning

12. Michael Schuerig Aug 31, 2009 at 13:34

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


Episode #177: Model Versioning

13. Chris Gunther Aug 31, 2009 at 19:42

Great screencast on a useful gem. Now I have to find a project to use it in.


Episode #177: Model Versioning

14. sthapit Sep 01, 2009 at 04:49

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!


Episode #177: Model Versioning

15. sam Sep 01, 2009 at 07:17

Hi, very nice screencast

can someone explain me what the & in

Page.find_each(&:touche)

does?

thx


Episode #177: Model Versioning

16. hartwig Sep 01, 2009 at 16:06

@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
 


Episode #177: Model Versioning

17. Aaron Sep 01, 2009 at 19:54

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


Episode #177: Model Versioning

18. Robert Sep 01, 2009 at 23:50

What version of Rails does vestal_versions require, will it work on 2.1?


Episode #177: Model Versioning

19. vidar Sep 02, 2009 at 03:36

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


Episode #177: Model Versioning

20. Vishnu Shashank Sep 03, 2009 at 04:27

I was using acts_as_versioned. Thanks Ryan. This one is good.
@Bryan, Thanks. I got the HTMLDiff working as well. Its fantastic.


Episode #177: Model Versioning

21. Matias Korhonen Sep 05, 2009 at 05:37

Maybe it's about time for a Railscast on anti-spam measures?


Episode #177: Model Versioning

22. Dji Sep 07, 2009 at 18:04

It might be time for a Railscast on implementing Captcha and eliminating spam.


Episode #177: Model Versioning

23. mikhailov Sep 10, 2009 at 07:33

It's pretty cool!
By the way, isn't hard implementation


Episode #177: Model Versioning

24. Everlast Sep 12, 2009 at 03:12

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


Episode #177: Model Versioning

25. sthapit Sep 24, 2009 at 02:11

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


Episode #177: Model Versioning

26. kb Oct 08, 2009 at 06:00

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)


Episode #177: Model Versioning

27. edimhudeemtren Mar 06, 2010 at 02:49

It,s a good material.


Episode #177: Model Versioning

28. mbt shuguli gtx Mar 28, 2010 at 20:22

Very interesting!Thank you for your sharing!


Episode #177: Model Versioning

29. daisy Apr 21, 2010 at 00:17

Life is just a series of trying to make up your mind.


Episode #177: Model Versioning

30. cly Apr 21, 2010 at 00:18

It is not enough to be industrious, so are the ants. What are you industrious for?


Episode #177: Model Versioning

31. lily Apr 21, 2010 at 02:14

    contented mind is the greatest blessing a man can enjoy in this world.


Episode #177: Model Versioning

32. lily Apr 21, 2010 at 02:15

 A lifetime of happiness!No man alive could bear it; it would be hell on erath.


Episode #177: Model Versioning

33. birdprotectblog Apr 29, 2010 at 20:01

Life can be good,
  Life can be bad,
  Life is mostly cheerful,
  But sometimes sad.
  Life can be dreams,
  Life can be great thoughts;
  Life can mean a person,
  Sitting in court.
  


Episode #177: Model Versioning

34. fbafblog Apr 29, 2010 at 20:01

Life can be dirty,
  Life can even be painful;
  But life is what you make it,
  So try to make it beautiful.


Episode #177: Model Versioning

35. bkod Apr 29, 2010 at 20:02

Hold fast to dreams,
  For if dreams die
  life is a broken-winged bird
  That cannot fly.


Episode #177: Model Versioning

36. 49p.org Apr 29, 2010 at 20:03

If your life feels like it is lacking the power that you want and the motivation that you need, sometimes all you have to do is shift your point of view


Episode #177: Model Versioning

37. bnco Apr 29, 2010 at 20:03

Hold fast to dreams,
  For when dreams go
  life is a barren field
  Forzen with snow.


Episode #177: Model Versioning

38. bvvv Apr 29, 2010 at 20:04

If your life feels like it is lacking the power that you want and the motivation that you need, sometimes all you have to do is shift your point of view.


Episode #177: Model Versioning

39. artslifeblog Apr 29, 2010 at 20:05

By training your thoughts to concentrate on the bright side of things, you are more likely to have the incentive to follow through on your goals.


Episode #177: Model Versioning

40. brandlifeblog Apr 29, 2010 at 20:06

You are less likely to be held back by negative ideas that might limit your performance.


Episode #177: Model Versioning

41. anffans Apr 29, 2010 at 20:07

Your life can be enhanced, and your happiness enriched, when you choose to change your perspective. Don't leave your future to chance


Episode #177: Model Versioning

42. brandshoeblog Apr 29, 2010 at 20:07

wait for things to get better mysteriously on their own. You must go in the direction of your hopes and aspirations. Begin to build your confidence, and work through problems rather than avoid them.


Episode #177: Model Versioning

43. mBddblog Apr 29, 2010 at 20:25

 It is true that the focus from the beginning has been on pragmatism, not necessarily understanding of the mechanisms of the actions - and that this has hindered its modern acceptance in the West.


Episode #177: Model Versioning

44. mbtsale Apr 29, 2010 at 20:54

all shall be well, jack shall have gill


Episode #177: Model Versioning

45. mbtshoes-shops Apr 29, 2010 at 20:56

So,don‘t envy other people because happiness is always nearly!


Episode #177: Model Versioning

46. abercrombie-1892 Apr 29, 2010 at 20:57

Instead,life is like a revolving door.when it closes,it also opens!


Episode #177: Model Versioning

47. abercrombie4u Apr 29, 2010 at 20:58

Pressure and challenges are all an opportunity for me to rise!


Episode #177: Model Versioning

48. jasmine Apr 29, 2010 at 22:13

One is always on a strange road, watching strange scenery and listening to strange music. Then one day, you will find that the things you try hard to forget are already gone.
[url=http://www.abercrombieandfitchs.com/]Abercrombie and Fitch[/url]


Episode #177: Model Versioning

49. jasmine Apr 29, 2010 at 22:15

<a href=" http://www.abercrombieandfitchs.com/" title="Abercrombie and Fitch">Abercrombie and Fitch</a>Happiness is not about being immortal nor having food or rights in one's hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love.


Episode #177: Model Versioning

50. jasmine Apr 30, 2010 at 01:45

One may fall in love with many people during the lifetime. When you finally get your own happiness, you will understand the previous sadness is kind of treasure, which makes you better to hold and cherishthe people you love.<a href="http://www.nikejordanpremium.com/">Air Jordan</a>


Episode #177: Model Versioning

51. jasmine Apr 30, 2010 at 01:47

One may fall in love with many people during the lifetime. When you finally get your own happiness, you will understand the previous sadness is kind of treasure, which makes you better to hold and cherishthe people you love.<a href="http://www.nikejordanpremium.com/">Air Jodan</a>


Episode #177: Model Versioning

52. blue May 21, 2010 at 18:30

<a href="http://www.timberlandonlineshop.com/">Timberland Boot</a> Pain past is pleasure.


Episode #177: Model Versioning

53. Timberland Boot Jun 19, 2010 at 18:16

 Pain past is pleasure.<a href="http://www.timberlandonlineshop.com/">Timberland Boot</a>


Episode #177: Model Versioning

54. rerewrwe Jun 20, 2010 at 19:05

gwgwrgergreger


Episode #177: Model Versioning

55. ersey Jun 21, 2010 at 00:20

[url=http://www.nflonlinestore.net]NFL[/url]


Episode #177: Model Versioning

56. NFL Jersey Jun 21, 2010 at 00:20

Thanks for sharing the nice post with me, an girl who firmly follows the fashion trend, It really brings me some fresh feelings with addtional insights into today's world.


Episode #177: Model Versioning

57. io Jun 23, 2010 at 12:08

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.


Episode #177: Model Versioning

58. webtasarim Jul 15, 2010 at 09:04

web tasarımı, kurumsal site tasarımı, profesyonel web sitesi tasarımı, profesyonel web tasarımı

<a href="http://www.webtasarimturk.net" title="web tasarımı">web tasarımı</a>



Episode #177: Model Versioning

60. bathroom vanity Jul 27, 2010 at 22:27

Good article, Each and every point is good enough.Thanks for sharing with us your wisdom.


Episode #177: Model Versioning

61. power strip Jul 27, 2010 at 22:29

I am totally agree with your oppinion.this blog post is very encouraging to people who want to know these topics.


Episode #177: Model Versioning

63. abercrombie london Aug 04, 2010 at 00:15

Wish there’s a way out for all of us, is there? I hope so.


Episode #177: Model Versioning

64. wholesale new era hats Aug 20, 2010 at 20:25

I really liked your article and I shared with my friends in my facebook account ..
I gave my site a few examples below. If you appreciate my comments in you enter.


Episode #177: Model Versioning

65. Nike Sb Dunks Aug 20, 2010 at 22:53

Thanks for the code. Good article, Each and every point is good enough. I really enjoy watching the RailsCasts. I think type of site that is useful in sharing information and it is important to share.


Episode #177: Model Versioning

66. PDF to Images Converter Aug 24, 2010 at 23:13

Some times, to a certain need, we have to convert PDF to image for enjoyment.


Episode #177: Model Versioning

67. christian louboutin simple pump Aug 25, 2010 at 00:36

That is a idea, I am agree with the opinions of this articles about


Episode #177: Model Versioning

68. Wholesale Electronics Aug 25, 2010 at 01:40

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


Episode #177: Model Versioning

69. louis vuitton shoes Aug 26, 2010 at 21:14

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


Episode #177: Model Versioning

70. louis vuitton shoes Aug 26, 2010 at 23:22

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


Episode #177: Model Versioning

71. wholesale Keyboard Aug 30, 2010 at 20:30

thanks for your sharing, it is so nice and i like it very much.


Episode #177: Model Versioning

72. snow boots Aug 30, 2010 at 20:49

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

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source