Skip to content

fix(sync): scope after_commit to prevent nil family error on destroy#1976

Draft
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/sync-callback-nil-family
Draft

fix(sync): scope after_commit to prevent nil family error on destroy#1976
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/sync-callback-nil-family

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry Bot commented May 25, 2026

This PR addresses a NoMethodError: undefined method 'family' for nil occurring in Sync#update_family_sync_timestamp.

Problem:
When InactiveFamilyCleanerJob destroys a Family record, associated Sync records are cascade-destroyed. The after_commit :update_family_sync_timestamp callback on the Sync model would then fire. Inside this callback, the family method attempts to access syncable.family. However, because the syncable (the polymorphic association target) had already been destroyed in the same transaction, syncable was nil, leading to the NoMethodError.

Solution:

  1. Scoped after_commit callback: The after_commit :update_family_sync_timestamp callback in app/models/sync.rb (line 22) was modified to after_commit :update_family_sync_timestamp, on: [:create, :update]. This ensures the callback only runs when a Sync record is created or updated, and not when it is destroyed, as updating latest_sync_activity_at is not relevant during destruction.
  2. Added nil guard to family method: A return nil unless syncable guard was added to the private family method in Sync (line 258). This provides a defense-in-depth mechanism, preventing NoMethodError if syncable is nil for any reason when family is called.
  3. Safe navigation in update_family_sync_timestamp: The return unless family.persisted? line in update_family_sync_timestamp (line 253) was updated to return unless family&.persisted? to safely handle cases where the family method might return nil due to the new guard.

Fixes SURE-APP-Y5

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.

0 participants