13

I just upgraded to Rails 5.1.1 and am receiving this error.

NoMethodError (undefined method `sanitize' for ActiveRecord::Base:Class):

The stack traces back to this code

like_search_term = ActiveRecord::Base::sanitize("%#{escaped_search_term}%")

Has this method been removed or changed in the new Rails upgrade?

William Holt
  • 549
  • 4
  • 14

2 Answers2

15

Yes, indeed, it appears to be removed.

Sanitize was never part of the public API of the framework. As we didn't need it in the framework anymore, we removed. The recommended ways to sanitize raw SQL for use in execute statements were the public API for that http://api.rubyonrails.org/classes/ActiveRecord/Sanitization/ClassMethods.html

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
0

You can still use the sanitization methods if you use them within context of the model. For example, you can add this to your model:

def self.where_ilike(search_terms)
  where('search_tokens ILIKE ?', "%#{sanitize_sql_like(search_terms)}%")
end
Jason L.
  • 1,125
  • 11
  • 19