Code Monkey home page Code Monkey logo

mover's Introduction

Mover

Move ActiveRecord records across tables like it ain't no thang.

Requirements

sudo gem install mover

Move records

Move the last article:

Article.last.move_to(ArticleArchive)

Move today's articles:

Article.move_to(
  ArticleArchive,
  :conditions => [ "created_at > ?", Date.today ]
)

The two tables do not have to be identical. Only shared columns transfer.

If a primary key collision occurs, the destination record is updated.

Callbacks

In this example, we want an "archive" table for articles and comments.

We also want the article's comments to be archived when the article is.

class Article < ActiveRecord::Base
  has_many :comments
  before_move :ArticleArchive do
    comments.each { |c| c.move_to(CommentArchive) }
  end
end

class ArticleArchive < ActiveRecord::Base
  has_many :comments, :class_name => 'CommentArchive', :foreign_key => 'article_id'
  before_move :Article do
    comments.each { |c| c.move_to(Comment) }
  end
end

class Comment < ActiveRecord::Base
  belongs_to :article
end

class CommentArchive < ActiveRecord::Base
  belongs_to :article, :class_name => 'ArticleArchive', :foreign_key => 'article_id'
end

The after_move callback is also available.

Magic column

If a table contains a moved_at column, it will magically populate with the date and time it was moved.

Options

There are other options, in addition to conditions:

Article.move_to(
  ArticleArchive,
  :copy => true,          # Do not delete Article after move
  :generic => true,       # UPDATE using a JOIN instead of ON DUPLICATE KEY UPDATE (default: false on MySQL engines)
  :magic => 'updated_at', # Custom magic column
  :migrate => true,       # Copies the original value of the magic column
  :quick => true          # You are certain only INSERTs are necessary, no primary key collisions possible
                          # May only be a little faster on MySQL, but dramatically faster on other engines
)

You can access these options from callbacks using move_options.

Reserve a spot

Before you create a record, you can "reserve a spot" on a table that you will move the record to later.

archive = ArticleArchive.new
archive.id = Article.reserve_id
archive.save

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.