Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The update_all method doesn't return the number of rows affected when combining the relation with in_batches. #40287

Closed
khiav223577 opened this issue Sep 25, 2020 · 0 comments · Fixed by #40293

Comments

@khiav223577
Copy link

Steps to reproduce

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"
  gem "activerecord", '~> 6.0.3'
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table "users", force: :cascade do |t|
  end
end

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

class User < ApplicationRecord
end

User.transaction do
  3.times do
    User.create
  end
end

class BugTest < Minitest::Test
  def test_update_all
    assert_equal 3, User.update_all('id = id')
  end

  def test_update_all_in_batches
    assert_equal 3, User.in_batches.update_all('id = id')
  end
end

=begin
Result:

  1) Failure:
BugTest#test_update_all_in_batches [test.rb:45]:
Expected: 3
  Actual: nil
=end

Expected behavior

Expect the update_all method always returns the number of rows affected.

Actual behavior

The return value of in_batches.update_all is nil.

System configuration

Rails version:
Rails 6.0.3.2

Ruby version:
ruby 2.6.4p104 (2019-08-28 revision 67798) [x86_64-linux]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants