What is the return value of update_all() in ActiveRecord / Ruby on Rails?

Viewed 4997

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?
2 Answers

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
Related