Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda