Skip to content

AR::Relation cache_key_with_version remains same when updated_at has changed #41784

@okuramasafumi

Description

@okuramasafumi

Steps to reproduce

require "bundler/inline"

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

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

  gem "rails", path: "./"
  # 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::Base.collection_cache_versioning = true

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

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

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

class BugTest < Minitest::Test
  def test_association_cache_key
    post = Post.create!
    post.comments << Comment.create!

    cache_key = post.comments.cache_key_with_version
    comment = post.comments.first
    comment.touch(:updated_at, time: Time.new(2021, 5, 1))
    new_cache_key = post.comments.cache_key_with_version
    refute_equal cache_key, new_cache_key
  end
end

Expected behavior

cache_key_with_version is different since max value of updated_at in the collection has changed.

Actual behavior

cache_key_with_version remains the same.

System configuration

Rails version: 6.1.3.1

Ruby version: 2.7.2

related issues/prs

#34378

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