Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when update_all fails?

There is update_all method in RoR

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

For example:

ActiveRecord::Base.transaction do
  users = User.active
  users.update_all avatar: 'blablablb'
end
like image 245
Gleb Vishnevsky Avatar asked May 01 '16 20:05

Gleb Vishnevsky


1 Answers

update_all is one of many methods that skip validations and callbacks. So any ActiveRecord validations simply won't be run when calling update_all.

However, if the update_all call doesn't conform to a constraint at the database level, or otherwise triggers an exception in your database, then ActiveRecord will throw a ActiveRecord::StatementInvalid exception and will break from the update_all without returning a value, like any other exception.

Other ActiveRecord methods that skip validations include:

  • decrement!
  • decrement_counter
  • increment!
  • increment_counter
  • toggle!
  • touch
  • update_attribute
  • update_column
  • update_counters
like image 69
Anthony E Avatar answered Nov 02 '22 15:11

Anthony E