[delegated_type] How to query a collection of entries including the (entryable) sub-entries.
I need to query a collection of entries based on a range of dates and then sum an attribute of the sub-entry using the delegated_type method. Something like this:
Entry.where(performed_at: DateTime.now.last_month.all_month).includes(:first_sub_entries or entryable).sum(:total)
I've tried different variations without success. I can retrieve a single record.
Entry.last.entryable.total
Here are the related models:
class Entry < ApplicationRecord
delegated_type :entryable, types: %w[FirstSubEntry SecondSubEntry]
validates :performed_at, presence: true
end
module Entryable
extend ActiveSupport::Concern
included do
has_one :entry, as: :entryable, touch: true, inverse_of: :entryable
end
end
class FirstSubEntry < ApplicationRecord
include Entryable
validates :total, presence: true
class
class SecondSubEntry < ApplicationRecord
include Entryable
validates :metric, presence: true
class