Files
sure/app/models/entryable.rb
Serge L 861a2d2d91 Fix NoMethodError on nil entryable in account activity feed (#1316)
* Fix NoMethodError when entry has nil entryable

Guard against orphaned entries where the entryable record has been
deleted but the entry still exists, preventing a crash on the
account show page.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Add dependent: :destroy to Entryable has_one :entry

The polymorphic has_one :entry association lacked a dependent option,
meaning if a Transaction/Trade/Valuation was ever deleted directly
(bypassing the Entry), the Entry would be left orphaned with a nil
entryable — causing NoMethodError in the activity feed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Add nil entryable guard to _split_group.html.erb

Same defensive check as _entry.html.erb for consistency.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-29 17:30:12 +02:00

32 lines
709 B
Ruby

module Entryable
extend ActiveSupport::Concern
TYPES = %w[Valuation Transaction Trade]
def self.from_type(entryable_type)
entryable_type.presence_in(TYPES).constantize
end
included do
include Enrichable
has_one :entry, as: :entryable, touch: true, dependent: :destroy
scope :with_entry, -> { joins(:entry) }
scope :visible, -> { with_entry.merge(Entry.visible) }
scope :in_period, ->(period) {
with_entry.where(entries: { date: period.start_date..period.end_date })
}
scope :reverse_chronological, -> {
with_entry.merge(Entry.reverse_chronological)
}
scope :chronological, -> {
with_entry.merge(Entry.chronological)
}
end
end