Merging two/three records in rails

merge, merging-data, ruby-on-rails, ruby-on-rails-3

Solution

Something like this

@user1 = User.find(1);
@user2 = User.find(2);

Contact.where("user_id = ?", @user2.id).update_all(:user_id => @user1.id)
@user2.destroy

in case of generalize solution place file /lib/acts_as_user_merge.rb

module UserMerge
  module ActsAsUserMerge
    module Base
      def self.included(klass)
        klass.class_eval do
          extend Config
        end
      end
    end

    module Config
        def acts_as_user_merge
            include ::UserMerge::ActsAsUserMerge::InstanceMethods
        end
    end

    module InstanceMethods    
      def merge(user)
        models = Array.new
        models_names = User.reflections.collect{|a, b| b.class_name if b.macro==:has_many}.compact
        models_names.each do |name|
          models << Object.const_get name
        end
        models.each do |model|
          model.where("user_id = ?", user.id).update_all(:user_id => self.id)
        end
        user.destroy
      end
    end
  end
end

::ActiveRecord::Base.send :include, ::UserMerge::ActsAsUserMerge::Base

how to use

User < ActiveRecord::Base
  has_many ...

  acts_as_user_merge

end

@user1.merge(@user2)

kinda messy and not tested but should give you an idea

Problem

I want to merge two profiles into one. What is the best way to do this in Rails. I have two profiles say `user1` and `user2` and there are at least 30 tables associated with them. Now i want to merge them together so that there should be one profile say `user1` and `user2` should get deleted but all the associated data of `user2` now should associate with `user1`. For example: suppose `user2` has two contacts and `user1` has 3 contacts after merging user `user1` should have 5 contacts.

Original source