SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

Acts_as_versioned Rails Plugin

       

Versioning models with the acts_as_versioned plugin


cd rails/app
./script/plugin install git://github.com/technoweenie/acts_as_versioned.git
./script/generate model post title:string body:text

In your model


class Post < ActiveRecord::Base
  acts_as_versioned
end

In db/migrate/****_create_posts.rb


  def self.up
    create_table :posts do |t|
      t.string :title
      t.text :body
      t.timestamps
    end
    Post.create_versioned_table
  end

def self.down drop_table :posts Post.drop_versioned_table end

Migrate your db

rake db:migrate
Usage

p = Post.create :body => "hello world"
p.body = "HELLO WORLD"
p.save

p.versions.size p.versions.last p.revert_to(p.versions.first) p.body # => hello world

*Quick Note If you want to revert to an older version in a controller or something, don't do

@post = @post.revert_to(2)
Revert_to method will return a TrueClass, Boolean type. Instead use
@post.revert_to(2)
This method will update the attributes for you and when you call them you'll get that version.

More information is available here http://ar-versioned.rubyforge.org/ and http://www.urbanhonking.com/ideasfordozens/archives/2006/02/learns_to_use_a_1.html

Tagged w/ #active record #pluginsruby on rails

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app