💎 Ruby on Rails
Intermediate
What is the difference between update_all and update in Rails?
Answer
Model.update(id, attrs) finds a record and calls save, running validations and callbacks. records.update_all(column: value) issues a single bulk UPDATE SQL statement for all matching records — it is extremely fast for large datasets but bypasses all validations, callbacks, and timestamps (updated_at is not changed unless explicitly included). Use update_all for batch data migrations, status changes on thousands of records, and anywhere callback overhead is undesirable. Always be cautious — if you need after_update callbacks to fire (e.g., cache invalidation), use individual update calls instead.