rails: When using both touch and counter_cache on a belongs_to association, updated_at on the associated object instance is not updated
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
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 16 (14 by maintainers)
I think that we should maybe document it here: https://github.com/rails/rails/blob/7b1f65e8f56738aaa8dd15f19d3ac5f4385ff229/activerecord/lib/active_record/associations.rb#L1750 Saying that calling
reloadis necessary to load the newupdated_atvalue, when:touchis used with:counter_cache.At this point, I don’t know if it’s actually an issue that needs to be fixed, or if it’s just that
reloadshould be called. In which case, maybe that this behavior should be documented.