Coder Social home page Coder Social logo

acts_as_archive's Introduction

ActsAsArchive

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

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

Requirements

This gem is intended to work with ActiveRecord version 3.0.0 and later.

Install

Gemfile:

gem 'acts_as_archive', :git => 'http://github.com/xxx/acts_as_archive.git'

Update models

Add acts_as_archive to your models:

class Article < ActiveRecord::Base
  acts_as_archive
end

Create archive tables

Add this line to a migration:

ActsAsArchive.update Article, Comment

Replace Article, Comment with your own models that use acts_as_archive

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

There is an alternate way to create archive tables if you don't like migrations.

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?

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.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, the ActsAsArchive.update call will automatically move your deleted records to the archive table (see Create archive tables).

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 < ActiveRecord::Base
  acts_as_archive :indexes => [ :id, :created_at, :deleted_at ]
end

Call ActsAsArchive.update upon adding new indexes (see Create archive tables).

Delete records without archiving

To destroy a record without archiving:

article.destroy!

To delete multiple records without archiving:

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

acts_as_archive's People

Contributors

look avatar radar avatar varyform avatar whatcould avatar winton avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

backupify

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.