Skip to content

counter_cache called twice after replace counter called #31493

@zhouguangming

Description

@zhouguangming

Steps to reproduce

unless File.exist?('Gemfile')
  File.write('Gemfile', <<-GEMFILE)
    source 'https://rubygems.org'
    gem 'rails', github: 'rails/rails'
    gem 'arel', github: 'rails/arel'
    gem 'sqlite3'
  GEMFILE

  system 'bundle install --verbose'
end

require 'rails'
require 'active_record'
require 'minitest'
require 'minitest/autorun'
require 'byebug'

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

ActiveRecord::Schema.define do
  create_table :posts do |t|
    t.integer :comments_count
  end

  create_table :users do |t|
    t.integer :comments_count
  end

  create_table :comments do |t|
    t.integer :post_id
    t.integer :user_id
    t.text :context
  end
end

class Post < ActiveRecord::Base
  has_many :comments
end

class User < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :user, counter_cache: true
  belongs_to :post, counter_cache: true
end

class BugTest < Minitest::Test
  def test_counter_cache
    user = User.create
    post = Post.create
    comment = Comment.create(post_id: post.id, user_id: user.id)

    another_post = Post.create

    puts '-----------'
    comment.update_attributes(post: another_post)
    puts '-----------'
  end
end
 -----------
D, [2017-12-18T00:32:31.238466 #13777] DEBUG -- :    (0.1ms)  begin transaction
D, [2017-12-18T00:32:31.239663 #13777] DEBUG -- :   Post Update All (0.2ms)  UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1 WHERE "posts"."id" = ?  [["id", 2]]
D, [2017-12-18T00:32:31.240561 #13777] DEBUG -- :   Post Update All (0.1ms)  UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) - 1 WHERE "posts"."id" = ?  [["id", 1]]
D, [2017-12-18T00:32:31.242359 #13777] DEBUG -- :   Comment Update (0.2ms)  UPDATE "comments" SET "post_id" = ? WHERE "comments"."id" = ?  [["post_id", 2], ["id", 1]]
D, [2017-12-18T00:32:31.243532 #13777] DEBUG -- :   Post Update All (0.1ms)  UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1 WHERE "posts"."id" = ?  [["id", 2]]
D, [2017-12-18T00:32:31.244237 #13777] DEBUG -- :   Post Update All (0.2ms)  UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) - 1 WHERE "posts"."id" = ?  [["id", 1]]
D, [2017-12-18T00:32:31.244655 #13777] DEBUG -- :    (0.1ms)  commit transaction
-----------

Expected behavior

Post update comments_count once

Actual behavior

Post update comments_count twice

System configuration

Rails version: 5.2.0.beta2 475a0f8

Ruby version:ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions