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

Use foreign_key to reference parent association #548

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix parent cache invalidation for custom foreign key associations

## 1.5.0

- Require Active Record 7+
Expand Down
2 changes: 1 addition & 1 deletion lib/identity_cache/parent_model_expiration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def add_record_to_cache_expiry_set(parents_to_expire, record)

def parents_to_expire_on_changes(parents_to_expire, association_name, cached_associations)
parent_association = self.class.reflect_on_association(association_name)
foreign_key = parent_association.association_foreign_key
foreign_key = parent_association.foreign_key

new_parent = send(association_name)

Expand Down
2 changes: 1 addition & 1 deletion test/custom_primary_keys_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class CustomPrimaryKeysTest < IdentityCache::TestCase
def setup
super
CustomParentRecord.cache_has_many(:custom_child_record)
CustomParentRecord.cache_has_many(:custom_child_records)
CustomChildRecord.cache_belongs_to(:custom_parent_record)
@parent_record = CustomParentRecord.create!(parent_primary_key: 1)
@child_record_1 = CustomChildRecord.create!(custom_parent_record: @parent_record, child_primary_key: 1)
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class StiRecordTypeA < StiRecord

class CustomParentRecord < ActiveRecord::Base
include IdentityCache
has_many :custom_child_record, foreign_key: :parent_id
has_many :custom_child_records, foreign_key: :parent_id
self.primary_key = "parent_primary_key"
end

Expand Down
29 changes: 29 additions & 0 deletions test/parent_model_expiration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,33 @@ def test_recursively_expire_parent_caches
fetched_name = Item.fetch(item.id).fetch_associated_records.first.fetch_deeply_associated_records.first.name
assert_equal("updated child", fetched_name)
end

def test_custom_parent_foreign_key_expiry
define_cache_indexes = lambda do
CustomParentRecord.cache_has_many(:custom_child_records, embed: true)
CustomChildRecord.cache_belongs_to(:custom_parent_record)
end
define_cache_indexes.call
old_parent = CustomParentRecord.new(parent_primary_key: 1)
old_parent.save!
child = CustomChildRecord.new(child_primary_key: 10, parent_id: old_parent.id)
child.save!

# Warm the custom_child_records embedded cache on the old parent record
assert_equal(10, CustomParentRecord.fetch(1).fetch_custom_child_records.first.child_primary_key)

new_parent = CustomParentRecord.new(parent_primary_key: 2)
new_parent.save!
# Warm the custom_child_records embedded cache on the new parent record
assert_empty(CustomParentRecord.fetch(2).fetch_custom_child_records)

# Now invoke a db update, where the child switches parent
child.parent_id = new_parent.parent_primary_key
child.save!

# the old parent's custom_child_records embedded cache should be invalidated and empty
assert_empty(CustomParentRecord.fetch(1).fetch_custom_child_records)
# the new parent's custom_child_records embedded cache should be invalidated and filled with the new association
assert_equal(10, CustomParentRecord.fetch(2).fetch_custom_child_records.first.child_primary_key)
end
end
Loading