Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created August 21, 2017 01:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongyuwei/12ae28ec03464fd851aa78e0814a13c0 to your computer and use it in GitHub Desktop.
Save dongyuwei/12ae28ec03464fd851aa78e0814a13c0 to your computer and use it in GitHub Desktop.
snapshot design for rails's ActiveRecord model
class CreateSnapShots < ActiveRecord::Migration[5.0]
def change
create_table :snap_shots do |t|
t.integer :user_id
t.string :table_name
t.text :table_data , limit: 4294967295
t.integer :table_id
t.integer :snapshot_id
t.timestamps
end
add_index :snap_shots, :user_id
add_index :snap_shots, :table_name
add_index :snap_shots, :table_id
add_index :snap_shots, :snapshot_id
end
end
@dongyuwei
Copy link
Author

class SnapShotsService < ApplicationService
  def self.create!(user_id, table_name, table_id, table_data, snapshot_id)
    SnapShot.create!({
        user_id: user_id,
        table_name: table_name,
        table_id: table_id,
        table_data: table_data,
        snapshot_id: snapshot_id
    })
  end
end

@dongyuwei
Copy link
Author

table_id actually is the record_id of the target table.
snapshot_id is the index of all relations of the snapshotted models.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment