From 101625ebab65f3a24e944659efd1f0980fb477df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 22 Jan 2021 17:54:17 +0100 Subject: [PATCH] Touch the associated record when assigning targets So the cache will expire properly. --- app/models/sdg/relation.rb | 2 +- spec/models/sdg/relatable_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/models/sdg/relation.rb b/app/models/sdg/relation.rb index 0148bea4665..dff71a0130d 100644 --- a/app/models/sdg/relation.rb +++ b/app/models/sdg/relation.rb @@ -1,6 +1,6 @@ class SDG::Relation < ApplicationRecord validates :related_sdg_id, uniqueness: { scope: [:related_sdg_type, :relatable_id, :relatable_type] } - belongs_to :relatable, polymorphic: true, optional: false + belongs_to :relatable, polymorphic: true, optional: false, touch: true belongs_to :related_sdg, polymorphic: true, optional: false end diff --git a/spec/models/sdg/relatable_spec.rb b/spec/models/sdg/relatable_spec.rb index 4dd6aea511f..d8131336717 100644 --- a/spec/models/sdg/relatable_spec.rb +++ b/spec/models/sdg/relatable_spec.rb @@ -159,6 +159,16 @@ expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[3], SDG::Goal[4]] expect(relatable.reload.sdg_targets).to match_array [SDG::Target[1.1], SDG::Target[4.1]] end + + it "touches the associated record" do + relatable.sdg_related_list = "1.1, 2.1, 2.2" + + travel(10.seconds) do + relatable.sdg_related_list = "1.1, 2.1, 2.2, 3.1" + + expect(relatable.updated_at).to eq Time.current + end + end end describe "#sdg_review" do