What happens when update_all fails?

RoR has an update_all method

And what does it return if update_all fails? will it lead to an exception?

For instance:

ActiveRecord::Base.transaction do users = User.active users.update_all avatar: 'blablablb' end 
+5
source share
1 answer

update_all is one of many methods that skip checks and callbacks. Therefore, any ActiveRecord checks will simply not be executed when update_all called.

However, if the update_all call does not meet database-level constraints or otherwise throws an exception in your database, then ActiveRecord will throw an ActiveRecord::StatementInvalid exception and will interrupt with update_all without returning a value like any other exception.

Other ActiveRecord methods that skip checks include:

  • decrement!
  • decment_counter
  • increase!
  • increment_counter
  • toggle switch!
  • click
  • update_attribute
  • update_column
  • update_counters
+4
source

All Articles