To unite together (classes, races, societies, ideas, etc.) so as to form a homogeneous or harmonious whole. (Used either of combining two elements, or one element with another.)
Source: "amalgamate, v.". OED Online. December 2012. Oxford University Press. 5 February 2013 http://www.oed.com/view/Entry/5975.
Amalgamate is an extension of ActiveRecord that facilitates merging two individual records into a single record. The intent is to provide a simple, fast, and safe method for reducing duplication within the database.
Add the following line to the project Gemfile
gem 'amalgamate', github: 'danreedy/amalgamate', branch: 'rails4'
Then run bundle install
.
Making use of ActiveSupport::Concern
, Amalgamate extends ActiveRecord objects to include several class methods. Refer to the documentation to see the underlining logic.
All examples will use the terms master and slave. Master being the record to keep and slave being the duplicate record.
Unify is the most frequently used method. Calling master.unify(slave)
will:
- Fill any nil attributes of
master
with the non-nil value of the same attribute ofslave
. - Use the attribute of
master
if there is a conflict between the same attribute ofslave
. - Update
has_one
andhas_many
associations ofslave
to reference tomaster
. - Call
destroy
onslave
- Call
save
onmaster
Option | Accepted Values | Default | Description |
---|---|---|---|
:priority |
:master , :slave |
:master |
Determines which object takes priority when setting attributes. If set to :slave the attribute values of slave will be used to update master |
:ignore |
Array of sybmols |
[:id, :created_at, :updated_at] |
Ignores attributes when merging. |
:reassign_associations |
true , false |
true |
Amalgamate will update has_one and has_many associations to change slave associations to point to master |
:save |
true , false |
true |
Amalgamate will call save on master |
:destroy |
true , false |
true |
Amalgamate will call destroy on slave after saving master |
Diff returns a Hash
containing the attributes that a different between master
and slave
.
Example Output
> master.first_name
=> "Dan"
> master.last_name
=> "Reedy"
> slave.first_name
=> "Daniel"
> slave.last_name
=> "Reedy"
> master.diff(slave)
=> { :first_name => { :master => "Dan", :slave => "Daniel" } }
This project rocks and uses MIT-LICENSE.