diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index 4b4e00a51c..f0c355bbfc 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -121,6 +121,8 @@ class Page < BaseRecord has_one :draft_version, -> { drafts }, class_name: "Alchemy::PageVersion" has_one :public_version, -> { published }, class_name: "Alchemy::PageVersion", autosave: -> { persisted? } + has_many :page_ingredients, class_name: "Alchemy::Ingredients::Page", foreign_key: :related_object_id, dependent: :nullify + before_validation :set_language, if: -> { language.nil? } diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index 51653cf939..57955f613d 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -222,6 +222,14 @@ module Alchemy it "destroys elements along with itself" do expect { page.destroy! }.to change(Alchemy::Element, :count).from(3).to(0) end + + context "with a page ingredient pointing to the page" do + let!(:ingredient) { create(:alchemy_ingredient_page, page: page) } + + it "nullifies the foreign key on the ingredient" do + expect { page.destroy! }.to change { ingredient.reload.related_object_id }.from(page.id).to(nil) + end + end end end