William Holt
William Holt

Reputation: 569

Undefined method #sanitize for ActiveRecord::Base

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?

Upvotes: 14

Views: 9525

Answers (2)

Jason L.
Jason L.

Reputation: 1215

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

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230481

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

Upvotes: 16

Related Questions