如果您真的需要编写原始SQL,可以使用quote它来清理它:
quote
conn = ActiveRecord::Base.connection name = conn.quote("John O'Neil") title = conn.quote(nil) query = "INSERT INTO users (name,title) VALUES (#{name}, #{title})" conn.execute(query)