You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: association_scope.rb
    path of file: ./repos/gitlabhq/lib/gem_extensions/active_record/disable_joins/associations
    
    the code of the file until where you have to start completion: # frozen_string_literal: true

module GemExtensions
  module ActiveRecord
    module DisableJoins
 
    
          def add_constraints(reflection, key, join_ids, owner, ordered, previous_reflection: nil)
            scope = reflection.build_scope(reflection.aliased_table).where(key => join_ids) # rubocop:disable CodeReuse/ActiveRecord

            # Pulled from https://github.com/rails/rails/pull/42590
            # Fixes cases where used with an STI type
            relation = reflection.klass.scope_for_association
            scope.merge!(
              relation.except(:select, :create_with, :includes, :preload, :eager_load, :joins, :left_outer_joins)
            )

            # Attempt to fix use case where we have a polymorphic relationship
            # Build on an additional scope to filter by the polymorphic type
            if reflection.type
              polymorphic_class = previous_reflection.try(:klass) || owner.class

              polymorphic_type = transform_value(polymorphic_class.polymorphic_name)
              scope = apply_scope(scope, reflection.aliased_table, reflection.type, polymorphic_type)
            end