mysqlsqlruby-on-rails

Rails - update directly in SQL


Delving into the documentation and the api, I seem to be missing how to update one field in multiple rows at once.

Something like

Table.select(:field).update("update to this").where(id: 4,5,6)

would be nice.

Does something like this exist? It would be much better than having to store everything in an array, set it to a value, and calling save every time.


Solution

  • You can use the update_all method, for example:

    Table.update_all("field = 'update to this'", ["id in (?)", ids])