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?
And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome on the rubyonrails-docs mailing list . "Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson.
For more information on the dangers of SQL injection, see the Ruby on Rails Security Guide. Similar to the (?) replacement style of params, you can also specify keys in your conditions string along with a corresponding keys/values hash:
Migrations are stored in files which are executed against any database that Active Record supports using rake. Here's a migration that creates a table: Rails keeps track of which files have been committed to the database and provides rollback features.
when return isn’t explicitly called within a method then Ruby returns the value of the last executed instruction in the method In the implicit_return method, as if true is always evaluated as true (mister obvious) then the last executed instruction is 42.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With