Skip to content

When using both touch and counter_cache on a belongs_to association, updated_at on the associated object instance is not updated #44479

@yan-hoose

Description

@yan-hoose

Hi everyone!
I ran into an issue with a counter cached belongs_to association.

In short, when I have an association with only touch: true, it works as expected:

class Comment < ActiveRecord::Base
  belongs_to :post, touch: true
end
# updated_at is updated on the Post instance after doing this
post.comments.create!

But when I add counter_cache: true to the same association, it stops working as expected:

class Comment < ActiveRecord::Base
  belongs_to :post, touch: true, counter_cache: true
end
# updated_at is NOT updated on the Post instance after doing this (it has been updated in the DB however)
post.comments.create!

Steps to reproduce

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  #gem "rails", "6.0.4.6"
  #gem "rails", "6.1.4.6"
  #gem "rails", "7.0.2.2"
  gem "rails", github: "rails/rails", branch: "main"
  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 :posts, force: true do |t|
    t.integer :comments_count, null: false, default: 0
    t.timestamps
  end

  create_table :comments, force: true do |t|
    t.integer :post_id
  end
end

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  # works as expected
  #belongs_to :post, touch: true

  # does not work as expected
  belongs_to :post, touch: true, counter_cache: true
end

class BugTest < Minitest::Test
  def test_post_instance_updated
    post = Post.create!
    previous_updated_at = post.updated_at.to_f.to_s

    post.comments.create!

    # expecting post.updated_at to be different from the previous value
    # after creating a new comment on this post
    assert previous_updated_at != post.updated_at.to_f.to_s
  end
end

Expected behavior

Updated updated_at should be reflected on the Post instance in addition to being updated in the database.

Actual behavior

Updated updated_at is not reflected on the Post instance. It is however correctly updated in the database.

System configuration

Rails version:

  • 6.0.4.6
  • 6.1.4.6
  • 7.0.2.2
  • main

Ruby version: 2.7.5

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