def build_constraint_with_polymorphism(
      klass, table, key, foreign_table, foreign_key
    )
      if reflection.polymorphic?
        build_constraint_without_polymorphism(
          klass, table, key, foreign_table, foreign_key
        )
        .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
      else
        build_constraint_without_polymorphism(
          klass, table, key, foreign_table, foreign_key
        )
      end
    end
def build_constraint_with_polymorphism(
      klass, table, key, foreign_table, foreign_key
    )
      if reflection.polymorphic?
        build_constraint_without_polymorphism(
          klass, table, key, foreign_table, foreign_key
        )
        .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
      else
        build_constraint_without_polymorphism(
          klass, table, key, foreign_table, foreign_key
        )
      end
    end
Reference https://github.com/rails/rails/commit/9b15db51b78028bfecdb85595624de4b838adbd1