ruby on rails - How to properly escape a regular expression in Active Record where clause -
i using active record
v4.2.1
query mysql
database using regular expression. create following sql:
select `users`.* `users` (email regexp '[@\.]gmail\.com')
however, cannot seem find correct way create sql using active record.
user.where("email regexp ?", "[@.]gmail.com").to_sql
yields
"select `users`.* `users` (email regexp '[@.]gmail.com')"
user.where("email regexp ?", "[@\.]gmail\.com").to_sql
yields
"select `users`.* `users` (email regexp '[@.]gmail.com')"
user.where("email regexp ?", "[@\\.]gmail\\.com").to_sql
yields
"select `users`.* `users` (email regexp '[@\\\\.]gmail\\\\.com')"
how active record escape sql?
the escaping looks wrong in console when using to_sql
, last example sends mysql:
select `users`.* `users` (email regexp '[@\\.]gmail\\.com')
you can confirm running actual query in console , checking log file.
Comments
Post a Comment