diff --git a/app/models/item.rb b/app/models/item.rb index 4b7a4feb..51fab75a 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -263,6 +263,10 @@ class Item < ApplicationRecord end def update_cached_fields + # Reload our associations, so they include any new records. + swf_assets.reload + + # Then, compute and save our cached fields. self.cached_occupied_zone_ids = occupied_zone_ids self.cached_compatible_body_ids = compatible_body_ids(use_cached: false) self.save! diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb index 08d1f8bd..c76ff167 100644 --- a/spec/models/item_spec.rb +++ b/spec/models/item_spec.rb @@ -90,9 +90,6 @@ RSpec.describe Item do before do item.swf_assets << build_item_asset(zones(:wings), body_id: 1) item.swf_assets << build_item_asset(zones(:wings), body_id: 2) - # HACK: I don't understand why the first asset is triggering the hooks - # for cached fields, but the second isn't? Idk, force an update. - item.update_cached_fields end it_behaves_like "a not-fully-modeled item" @@ -112,9 +109,6 @@ RSpec.describe Item do item.swf_assets << build_item_asset(zones(:wings), body_id: 2) item.swf_assets << build_item_asset(zones(:wings), body_id: 3) item.swf_assets << build_item_asset(zones(:wings), body_id: 4) - # HACK: I don't understand why the first asset is triggering the hooks - # for cached fields, but the second isn't? Idk, force an update. - item.update_cached_fields end it_behaves_like "a fully-modeled item" @@ -155,9 +149,6 @@ RSpec.describe Item do before do item.swf_assets << build_item_asset(zones(:wings), body_id: 11) item.swf_assets << build_item_asset(zones(:wings), body_id: 12) - # HACK: I don't understand why the first asset is triggering the hooks - # for cached fields, but the second isn't? Idk, force an update. - item.update_cached_fields end it_behaves_like "a not-fully-modeled item" @@ -177,9 +168,6 @@ RSpec.describe Item do item.swf_assets << build_item_asset(zones(:wings), body_id: 12) item.swf_assets << build_item_asset(zones(:wings), body_id: 13) item.swf_assets << build_item_asset(zones(:wings), body_id: 4) - # HACK: I don't understand why the first asset is triggering the hooks - # for cached fields, but the second isn't? Idk, force an update. - item.update_cached_fields end it_behaves_like "a fully-modeled item"