7

我想将两个配置文件合并为一个。在 Rails 中执行此操作的最佳方法是什么。

我有两个配置文件user1user2并且至少有 30 个表与它们相关联。

现在我想将它们合并在一起,以便应该有一个配置文件说user1并且user2应该被删除,但现在的所有关联数据都user2应该user1

例如:假设user2有两个联系人,user1合并后有 3 个联系人,用户user1应该有 5 个联系人。

4

3 回答 3

7

像这样的东西

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

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

如果是通用解决方案放置文件 /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

如何使用

User < ActiveRecord::Base
  has_many ...

  acts_as_user_merge

end

@user1.merge(@user2)

有点凌乱,没有经过测试,但应该给你一个想法

于 2012-12-26T13:27:44.143 回答
1

类似的东西

def merge_users(dead, user)
  User.reflections.each do |assoc, reflection|
    foreign_key = reflection.foreign_key
    case reflection.macro
    when :has_many, :has_one then
      unless reflection.options[:through]
        reflection.klass.where(foreign_key => dead.id).update_all(foreign_key => user.id) # if id is a primary key
      end
      if options[:as] # polymorphic
        if reflection.macro == :has_many
          dead.send("#{options[:as].pluralize}")).each { |r| user.send("#{options[:as].pluralize}<<", r) }
        else
          user.send("#{options[:as]}=", dead.send("#{options[:as]}"))
          user.save
        end
      end
    when :belongs_to then
      if options[:polymorphic]
        user.send("#{assoc}=", deaf.send(assoc))
        user.save
      else
        user.update_attribute(foreign_key, dead.send(foreign_key))
      end
    when :has_and_belongs_to_many then
      dead.send("#{assoc}")).each { |r| user.send("#{assoc}<<", r) }
    end

  end
end

merge_users(dead_user, user)
dead_user.destroy
于 2012-12-26T17:29:48.437 回答
1

本文深入讨论了这个问题,并提供了它的工作代码: http: //ewout.name/2010/04/generic-deep-merge-for-activerecord/

于 2013-06-07T07:08:55.830 回答