Skip to content
forked from grosser/ar_merge

Merge 2 ActiveRecords, preserving attributes, associations and counters

Notifications You must be signed in to change notification settings

smabbott/ar_merge

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  • 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

before_save callback

  user.merge!(other) do |user, other|
    ...
  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
[email protected]
Build Status
License: MIT

About

Merge 2 ActiveRecords, preserving attributes, associations and counters

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%