Skip to content

AO3-4693 Fix collection deletion for collections with a challenge#5703

Open
ayasayadi1 wants to merge 8 commits intootwcode:masterfrom
ayasayadi1:AO3-4693
Open

AO3-4693 Fix collection deletion for collections with a challenge#5703
ayasayadi1 wants to merge 8 commits intootwcode:masterfrom
ayasayadi1:AO3-4693

Conversation

@ayasayadi1
Copy link
Copy Markdown
Contributor

Pull Request Checklist

Issue

https://otwarchive.atlassian.net/browse/AO3-4693

Purpose

This PR fixes deletion for collections with an associated challenge (gift exchange or prompt meme) by destroying the challenge first before the collection itself.

Credit

Aya Sayadi

@ayasayadi1 ayasayadi1 changed the title Fix collection deletion for collections with a challenge AO3-4693 Fix collection deletion for collections with a challenge Apr 4, 2026
@redsummernight redsummernight self-assigned this Apr 9, 2026
end

belongs_to :challenge, dependent: :destroy, polymorphic: true
belongs_to :challenge, polymorphic: true
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can fix this just with callbacks. In collection, you can keep the association as-is:

belongs_to :challenge, dependent: :destroy, polymorphic: true

In both challenge models, you can update the association to use :nullify.

has_one :collection, as: :challenge, dependent: :nullify

(which means you can fully remove all references of clear_challenge_references).

Rails 5+ should allow this bi-directional association to work (rails/rails#21350).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thank you for reviewing!

dependent: :nullify clears the FK. But since we now bypass ActiveRecord callbacks, clean_up_challenge never fires. As a result, signups, assignments, prompts etc survive the deletion. Some GE and PM feature tests caught this.

Previously, the flow was:

  1. before_destroy :clear_challenge_references -> challenge references cleared -> collection.save!
  2. after_save :clean_up_challenge -> associated records deleted
  3. challenge deleted

Now it's:

  1. challenge deleted
  2. :nullify -> challenge references cleared
  3. associated records remain

I worked around this by moving clean_up_challenge into ChallengeCore and calling it via before_destroy, which restores the original behaviour

That said, I think this workflow is still risky: if the challenge deletion fails after before_destroy runs, we've already lost the associated records. Please lmk what you think :)

it "destroys the collection and its gift exchange challenge" do
challenge = create(:gift_exchange)
collection = create(:collection, challenge: challenge)
collection.destroy
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use destroy! in specs, that way if there are errors you can easily see it in test logs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update gift_exchange_spec.rb and prompt_meme_spec.rb to test that a collection's challenge_id and challenge_type fields are nulled when the associated challenge is deleted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants