David Hempy
David Hempy

Reputation: 6277

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?

Upvotes: 19

Views: 8225

Answers (2)

Patrick
Patrick

Reputation: 1387

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

Upvotes: 3

David Hempy
David Hempy

Reputation: 6277

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

Upvotes: 23

Related Questions