rails icon indicating copy to clipboard operation
rails copied to clipboard

`counter_cache` not decrementing with has_many through association

Open zbokostya opened this issue 5 months ago • 5 comments

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', '7.1.1'
  # gem 'activerecord', '6.1.7.6'
  # gem 'activerecord', '5.2.8.1'
  gem 'sqlite3'
end

require 'active_record'
require 'minitest/autorun'
require 'logger'

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :books, force: true do |t|
  end

  create_table :parts, force: true do |t|
    t.integer :book_id
    t.integer :series_id
  end

  create_table :series, force: true do |t|
    t.integer :books_count, default: 0
  end
end

class Book < ActiveRecord::Base
  has_many :parts
  has_many :series, through: :parts
end

class Part < ActiveRecord::Base
  belongs_to :book
  belongs_to :series, counter_cache: :books_count
end

class Series < ActiveRecord::Base
  has_many :parts
  has_many :books, through: :parts
end

class BugTest < Minitest::Test

  def test_counter_cache_after_assign_and_clear
    book = Book.create!
    series = Series.create!

    series.books = [book]
    series.books = []
    assert_equal 0, series.books_count
  end

  def test_counter_cache_after_assign_and_clear_with_reload
    book = Book.create!
    series = Series.create!

    series.books = [book]
    series.books = []
    assert_equal 0, series.reload.books_count
  end
end

Expected behavior

counter_cache should become to eq 0 after assign has_many field with empty array

Actual behavior

counter_cache not decrementing

System configuration

Rails version: 7.1.1, 6.1.7.6, 5.2.8.1 Ruby version: 3.2.2, 2.7.5

zbokostya avatar Nov 22 '23 17:11 zbokostya