Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Don't delete your records, move them to a different table
tree: 728dae9a84

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bin
config
lib
rails
spec
.gitignore
LICENSE
README.md
Rakefile
acts_as_archive.gemspec
init.rb

README.md

ActsAsArchive

Don't delete your records, move them to a different table.

Like acts_as_paranoid, but doesn't mess with your SQL queries.

Install

gem install acts_as_archive

Rails 2

config/environment.rb

config.gem 'acts_as_archive'

Rails 3

Gemfile

gem 'acts_as_archive'

Sinatra

require 'acts_as_archive'

Add to models

Add acts_as_archive to your models:

class Article < ActiveRecord::Base
  acts_as_archive
end

Migrate

Next time you run rake db:migrate, your archive tables will be created automatically.

That's it!

Use destroy, destroy_all, delete, and delete_all like you normally would.

Records move into the archive table instead of being destroyed.

If any of your model's relationships have the :dependent option and also use acts_as_archive, those relationships will archive automatically.

What if my schema changes?

New migrations are automatically applied to the archive table.

No action is necessary on your part.

Query the archive

Add ::Archive to your ActiveRecord class:

Article::Archive.first

Delete records without archiving

Use any of the destroy methods, but add a bang (!):

Article::Archive.first.destroy!
Article.delete_all!([ "id in (?)", [ 1, 2, 3 ] ])

Restore from the archive

Use any of the destroy/delete methods on the archived record to move it back to its original table:

Article::Archive.first.destroy
Article::Archive.delete_all([ "id in (?)", [ 1, 2, 3 ] ])

Magic columns

You will find an extra deleted_at datetime column on the archive table.

You may manually add a restored_at datetime column to the origin table if you wish to store restoration time as well.

Migrate from acts_as_paranoid

Add this line to a migration, or run it via script/console:

Article.migrate_from_acts_as_paranoid

This copies all records with non-null deleted_at values to the archive.

Running specs

Contributions are always welcome.

gem install acts_as_archive --development
spec/run

spec/run essentially runs the following:

ACTIVERECORD=3 spec -f n -c spec
ACTIVERECORD=2 spec -f n -c spec/acts_as_archive_spec.rb
RAILS=2 spec -f n -c spec/acts_as_archive_spec.rb
RAILS=3 spec -f n -c spec/acts_as_archive_spec.rb
SINATRA=1 spec -f n -c spec/acts_as_archive_spec.rb

If you want to hack on the supporting gems (mover and also_migrate), vendor them with the following command:

externals un
Something went wrong with that request. Please try again.