def deep_merge(arg1, arg2, key=nil)
      # Attempt deep merge if the two arguments are
      # instances of the same class.
      if arg1.class == arg2.class
        # Preserve arg1's :code and :name.
        if (not key.nil?) and [:code, :name].include? key.to_sym
          return arg1
        # Recursively call deep merge for Hash objects.
        elsif arg1.respond_to? :to_hash
          r = {}
          return arg1.to_hash.merge(arg2.to_hash) do |key, oldval, newval|
            r[key] = deep_merge(oldval, newval, key)
          end
        # Concatenate Arrays and return uniq elements.
        elsif arg1.instance_of? Array
          return arg1.concat(arg2).uniq
        # If Institutions responds to the :key_add method,
        # go ahead and use that to add the Objects together.
        elsif respond_to? "#{key}_add".to_sym
          return send "#{key}_add".to_sym, arg1, arg2
        end
      end
      # NOTE: Commenting this out since I'm not sure this is desirable functionality.
      # If the two args aren't the same class, but arg1 is
      # Array, append arg2 to arg1.
      # if arg1.instance_of? Array
      #   return arg1.dup<<arg2
      # end
      # We tried.
      # If all else fails just replace arg1 with arg2.
      return arg2
    end
def deep_merge(arg1, arg2, key=nil)
      # Attempt deep merge if the two arguments are
      # instances of the same class.
      if arg1.class == arg2.class
        # Preserve arg1's :code and :name.
        if (not key.nil?) and [:code, :name].include? key.to_sym
          return arg1
        # Recursively call deep merge for Hash objects.
        elsif arg1.respond_to? :to_hash
          r = {}
          return arg1.to_hash.merge(arg2.to_hash) do |key, oldval, newval|
            r[key] = deep_merge(oldval, newval, key)
          end
        # Concatenate Arrays and return uniq elements.
        elsif arg1.instance_of? Array
          return arg1.concat(arg2).uniq
        # If Institutions responds to the :key_add method,
        # go ahead and use that to add the Objects together.
        elsif respond_to? "#{key}_add".to_sym
          return send "#{key}_add".to_sym, arg1, arg2
        end
      end
      # NOTE: Commenting this out since I'm not sure this is desirable functionality.
      # If the two args aren't the same class, but arg1 is
      # Array, append arg2 to arg1.
      # if arg1.instance_of? Array
      #   return arg1.dup<<arg2
      # end
      # We tried.
      # If all else fails just replace arg1 with arg2.
      return arg2
    end
Recursively merges Hash (or any Object that can be converted to a Hash),
 Array and attributes that have implemented an :attribute_add method.