How to sanitize sql fragment in Rails

dimus picture dimus · Jun 9, 2010 · Viewed 39.9k times · Source

I have to sanitize a part of sql query. I can do something like this:

class << ActiveRecord::Base
  public :sanitize_sql
end

str = ActiveRecord::Base.sanitize_sql(["AND column1 = ?", "two's"], '')

But it is not safe because I expose protected method. What is a better way to do it?

Answer

HashDog Team picture HashDog Team · Jun 28, 2012

You can just use:

ActiveRecord::Base::sanitize_sql(string)