You can use the ActiveRecord method quote(for example, ActiveRecord::Base.connection.quote("string with ' apostrophe")), but ActiveRecord query methods already avoid your SQL code for you. For instance:
a = "string with ' apostrophe"
ModelName.where("field1 = ?", a)
will change a line with an apostrophe to a line with an apostrophe
source
share