The Rails API does not seem to provide methods for this in general. You can try to access the base connection and use its methods, for example. for MySQL:
st = ActiveRecord::Base.connection.raw_connection.prepare("update table set f1=? where f2=? and f3=?") st.execute(f1, f2, f3) st.close
I am not sure if there are other consequences to this (connections remain open, etc.). I would follow the Rails code for a normal update to see what it does besides the actual request.
Using prepared queries can save you a small amount of time in the database, but if you do it a million times in a row, you probably would be better off just creating an update with a regular Ruby replacement, for example.
ActiveRecord::Base.connection.execute("update table set f1=#{ActiveRecord::Base.sanitize(f1)}")
or using ActiveRecord, as commentators noted.
Brian Deterling Dec 19 2018-10-18 18:54
source share