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
Ruby
tree: 31e9a0713e

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bin
lib
rails
spec
.gitignore
MIT-LICENSE
README.markdown
Rakefile
acts_as_archive.gemspec
init.rb

README.markdown

ActsAsArchive

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

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

Compatibility

Tested with Ruby 1.8.6, 1.8.7, and 1.9.1.

Install

sudo gem install acts_as_archive --source http://gemcutter.org

environment.rb:

config.gem 'acts_as_archive'

Update models

Add acts_as_archive to your models:

class Article 

Run acts_as_archive

Terminal:

cd your_rails_app
acts_as_archive Article

This command creates your archive tables (archived_articles as per the example).

Archive tables mirror your table's structure, but with an additional deleted_at column.

Run this command every time you add acts_as_archive to a new model.

That's it!

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

Records move into the archive table instead of being destroyed.

What if my schema changes?

Any new migrations on your table are automatically applied to the archive table.

Query the archive

Add ::Archive to your ActiveRecord class:

Article::Archive.find(:first)

Restore from the archive

Use restore_all to copy archived records back to your table:

Article.restore_all([ 'id = ?', 1 ])

Auto-migrate from acts_as_paranoid

If you previously used acts_as_paranoid, running the acts_as_archive command will automatically move your deleted records to the archive table (see Run acts_as_archive).

Original deleted_at values are preserved.

Add indexes to the archive table

To keep insertions fast, there are no indexes on your archive table by default.

If you are querying your archive a lot, you will want to add indexes:

class Article  [ :id, :created_at, :deleted_at ]
end

Run the acts_as_archive command any time you add new indexes (see Run acts_as_archive).

Something went wrong with that request. Please try again.