Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sanitize sql fragment in Rails

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?

like image 382
dimus Avatar asked Jun 09 '10 19:06

dimus


2 Answers

You can just use:

ActiveRecord::Base::sanitize_sql(string) 
like image 122
HashDog Team Avatar answered Sep 28 '22 07:09

HashDog Team


ActiveRecord::Base.connection.quote does the trick in Rails 3.x

like image 41
dimus Avatar answered Sep 28 '22 07:09

dimus