From 1912b8fbf0e1e1ea84a89c033e20e34235d19cce Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 21 Jul 2026 14:24:48 +0200 Subject: Stop related_assets from duplicating on every clone self.assets = page.assets implicitly cleared the collection before recreating it. Replacing it with an explicit loop, to preserve the headline flag across clones, dropped that clearing -- every clone_attributes_from call since has been appending related_assets rather than replacing them. Restored with an explicit delete_all before the copy. update_assets (the sort_images reorder action) had the same gap for headline specifically; it now carries the previous headline forward when that asset is still present in the new list. --- app/models/page.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/models/page.rb b/app/models/page.rb index f33d88d..ab42b1c 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -220,7 +220,12 @@ class Page < ApplicationRecord end # Clone asset references - self.assets = page.assets + self.related_assets.delete_all + page.related_assets.each do |related| + self.related_assets.create!(:asset_id => related.asset_id, + :position => related.position, + :headline => related.headline) + end self.save end @@ -315,18 +320,19 @@ class Page < ApplicationRecord end def update_assets image_ids - transaction do + previous_headline_asset_id = self.related_assets.find_by(headline: true)&.asset_id + self.related_assets.delete_all if image_ids image_ids.each_with_index do |id, index| asset = Asset.find(id) - self.related_assets.create!(:asset_id => asset.id, :position => index+1) + self.related_assets.create!(:asset_id => asset.id, :position => index+1, + :headline => asset.id == previous_headline_asset_id) end end end - end # Installs (or re-installs) the trigger that keeps page_translations' -- cgit v1.3