William Cunningham

Joined

24,970 Experience
249 Lessons Completed
0 Questions Solved

Activity

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

Posted in Rails 7.1 Authentication From Scratch Discussion

Question: How does defining helper methods as private methods (11:39) allow those methods to be accessible to views?

Posted in Rails view_components

+1