'What is the return value of update_all() in ActiveRecord / Ruby on Rails?
The Ruby on Rails and ActiveRecord documentation, Google, and StackOverflow are conspiratorially silent on the return value of update_all()
What does update_all() return?
- Number of records?
- Success status?
- ID's of updated records?
Solution 1:[1]
ActiveRecord's update_all() returns the number of records updated.
describe '.update_all' do
let!(:user1) { create :user, last_name: 'Smitty' }
let!(:user2) { create :user, last_name: 'Smitty' }
let!(:user3) { create :user, last_name: 'Doe' }
it 'returns number of records updated' do
expect(User.where(last_name: 'Smitty')
.update_all(last_name: 'Smith')).to eq 2
end
end
Yields:
User
.update_all
returns number of records updated
Finished in 0.1245 seconds (files took 13.17 seconds to load)
1 example, 0 failures
Solution 2:[2]
The documentation has been updated:
Returns the number of rows affected.
https://api.rubyonrails.org/v6.1.4/classes/ActiveRecord/Relation.html#method-i-update_all
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Patrick |