Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!