0

I am trying to run a SQL query on a model with a value that has a colon ':' in it.

Something like:

Model.find_by_sql([SELECT * FROM table WHERE field1 = ? AND field2 ='some:value'], params)

This causes the following error:

ActiveRecord::PreparedStatementInvalid

Any one know how to properly escape the colon ':' in the value text to field2 above?

2 Answers 2

4

Use where, it will make the code shorter:

Model.where("field1 = ? AND field2 ='some:value']", params)

Concrete example:

1.9.3-head :024 > Tag.where('created_at <= ? and name = "some:value"', Time.now)
  Tag Load (1.6ms)  SELECT "tags".* FROM "tags" WHERE (created_at <= '2012-04-23 18:06:54.967319' and name = "some:value")
 => [#<Tag id: 2419, name: "some:value", created_at: "2012-04-23 18:06:35", updated_at: "2012-04-23 18:06:35">] 
Sign up to request clarification or add additional context in comments.

Comments

2

gsub!(':','\\:\\') on value should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.