Skip to content

ActiveRecord::Relation#cache_key returns same key for different records with hm:t association #27570

Description

@javan

has_many :through relations that return the same number of records and one common record with the max updated_at will generate the same cache_key. For example:

class Access < ActiveRecord::Base; end
class Person < ActiveRecord::Base; end
class Bucket < ActiveRecord::Base
  has_many :people, through: :accesses
end
>> bucket.people.map(&:name)
=> ["Victor Cooper", "Jared Davis"]

>> bucket.people.cache_key
=> "people/query-2343a4c885c823c01ef9c2eb358b73eb-2-20161229150259317823"

Above, Victor Cooper has the max updated_at (20161229150259317823). If we remove Jared Davis and add a new person who's updated_at is less than Victor's, we'll get the same cache_key:

>> bucket.reload.people.map(&:name)
=> ["Victor Cooper", "Amy Rivera"]

>> bucket.people.cache_key
=> "people/query-2343a4c885c823c01ef9c2eb358b73eb-2-20161229150259317823"

This was introduced in #20884. /cc @afcapel @sgrif

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions