module ActiveRecordUpsert
  module ActiveRecord
    module RelationExtensions
      def upsert(existing_attributes, upsert_attributes, wheres) # :nodoc:
        im = arel_table.create_insert
        im.into arel_table

        substitutes, binds = substitute_values(existing_attributes)
        column_arr = self.klass.upsert_keys || [primary_key]
        column_name = column_arr.join(',')

        cm = arel_table.create_on_conflict_do_update
        cm.target = arel_table[column_name]
        cm.wheres = wheres
        filter = ->(o) { [*column_arr, 'created_at'].include?(o.name) }
        filter2 = ->(o) { upsert_attributes.include?(o.name) }

        vals_for_upsert = substitutes.reject { |s| filter.call(s.first) }
        vals_for_upsert = vals_for_upsert.select { |s| filter2.call(s.first) }

        cm.set(vals_for_upsert)
        on_conflict_binds = binds.reject(&filter).select(&filter2)

        im.on_conflict = cm.to_node

        im.insert substitutes


        @klass.connection.upsert(
          im,
          'SQL',
          nil, # primary key (not used)
          nil, # primary key value (not used)
          nil,
          binds + on_conflict_binds)
      end
    end
  end
end

module ActiveRecordUpsert
  module ActiveRecord
    module RelationExtensions
      def upsert(existing_attributes, upsert_attributes, wheres) # :nodoc:
        substitutes, binds = substitute_values(existing_attributes)
        upsert_keys = self.klass.upsert_keys || [primary_key]

        upsert_attributes = upsert_attributes - [*upsert_keys, 'created_at']
        upsert_keys_filter = ->(o) { upsert_attributes.include?(o.name) }

        on_conflict_binds = binds.select(&upsert_keys_filter)
        vals_for_upsert = substitutes.select { |s| upsert_keys_filter.call(s.first) }

        on_conflict_do_update = arel_table.create_on_conflict_do_update
        on_conflict_do_update.target = arel_table[upsert_keys.join(',')]
        on_conflict_do_update.wheres = wheres
        on_conflict_do_update.set(vals_for_upsert)

        insert_manager = arel_table.create_insert
        insert_manager.into arel_table
        insert_manager.on_conflict = on_conflict_do_update.to_node
        insert_manager.insert substitutes

        @klass.connection.upsert(
          insert_manager,
          'SQL',
          nil, # primary key (not used)
          nil, # primary key value (not used)
          nil,
          binds + on_conflict_binds)
      end
    end
  end
end

Make upsert method a bit more easy to read
Make upsert method a bit more easy to read
