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

Counter cache updates wrong column with :through relation #30484

Closed
r00takaspin opened this issue Aug 31, 2017 · 1 comment
Closed

Counter cache updates wrong column with :through relation #30484

r00takaspin opened this issue Aug 31, 2017 · 1 comment
Labels

Comments

@r00takaspin
Copy link

Steps to reproduce

# frozen_string_literal: true

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 "rails", github: "rails/rails"
  gem "arel", github: "rails/arel"
  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 :journals, force: true do |t|
    t.integer :issues_count, default: 0
    t.integer :articles_count, default: 0
  end

  create_table :issues, force: true do |t|
    t.integer :journal_id
    t.integer :articles_count
  end

  create_table :articles, force: true do |t|
    t.integer :issue_id
  end
end

class Journal < ActiveRecord::Base
  has_many :issues
  has_many :articles, through: :issues
end

class Issue < ActiveRecord::Base
  belongs_to :journal, counter_cache: true
  has_many :articles
end

class Article < ActiveRecord::Base
  belongs_to :issue, counter_cache: true
end

class BugTest < Minitest::Test
  def test_counter_cache
    journal = Journal.create!
    journal.issues << Issue.create!(articles: [Article.create!, Article.create!])

    Journal.reset_counters(journal.id, :articles)

    assert_equal journal.reload.articles_count, 2
  end
end

Expected behavior

Counter articles_count will be updated

Actual behavior

Instead of articles_count counter column, call of #reset_counters updates issues_count counter column

System configuration

Rails version: master

Ruby version: 2.4.0

@rails-bot rails-bot bot added the stale label Nov 29, 2017
@rails-bot
Copy link

rails-bot bot commented Nov 29, 2017

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot closed this as completed Dec 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant