Merge 2 ActiveRecords, preserving attributes, associations and counters
Ruby
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
gemfiles
lib
spec
.travis.yml
Appraisals
Gemfile
Gemfile.lock
Rakefile
Readme.md
ar_merge.gemspec

Readme.md

  • Merges associations/attributes you want
  • Can merge duplicates
  • Protects from self-merges
  • Keeps counters valid
  • Removes merged record

INSTALL

gem install ar_merge

USAGE

Merge from outside the model:

user.merge!(other,:attributes=>user.attributes.keys,:associations=>%w[movies friends])`

Merge from inside the model

User < ActiveRecord::Base
  def merge!(other)
    super(other,:attributes=>%w[email website])
  end
end

Merge duplicates

#merge all new users, that have the same email
User.merge_duplicates!(User.find_all_by_status('new')) , :compare=>:email)

AUTHOR

Michael Grosser
michael@grosser.it
Build Status
License: MIT