Rails LIKE query using mysql -
i'm trying query matching substrings. used this:
products = product.where("title ? or description ?", "%#{params[:s_term]}%", "%#{params[:s_term]}%")
which worked great sqlite3 (not worried sql injection right now). however, switched mysql, , when perform query whines encoding::undefinedconversionerror ("\xc2" ascii-8bit utf-8): because of '%' in query. there way me escape or differently in query?
after odesy of encoding "adventures", problem turned out file "importing" database contained strings not utf8 , ascii compatible (the problem character: Â). in function using add strings database, used keep these characters entering db:
thestring.gsub(/\p{ascii}/, '').encode('utf-8')
now can sure in database encoded correctly , searches not return results rails cannot handle encoding reasons!
an alternative may :
thestring.delete!("^\u{0000}-\u{007f}").encode('utf-8')
this alternative did not work me, may work ruby/rails versions.
Comments
Post a Comment