Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
Escape only some characters in where clause

I want to fetch records that have some string field start with a given prefix and end on any one character. Basically:

 Model.where('field LIKE ?', "#{prefix}_").count

The problem is that the prefix itself might contain special characters (like % or _).

Is there a way to escape the prefix, but not the trailing _ without rolling my own sanitizer with a bunch of #gsubs?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

There is no better solution than replacing all _ with \_ and all % with \% to escape their special meaning.

over 4 years ago · Santiago Trujillo Denunciar

0

Model.where("field LIKE ?||'_'", escapeDataFunction("#{prefix}")).count

The idea is to escape what needs to be escaped and hard code the other part in the "where" condition. Also note that when using substitution variables (? or :1), then the data need not be escaped at all in general, but "like" expressions are an exception, and in that case, you should escape the special characters with meaning in the like operator.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda