millions-exabyte-27721
04/07/2025, 9:47 AMrecipients in a list:
rb
class Mailing < ApplicationRecord
belongs_to :mailable, polymorphic: true
delegate :recipients, to: :mailable
end
and an example of a `mailable`:
rb
class Event < ApplicationRecord
has_many :recipients
has_many :mailings, as: :mailable
end
The delegate is necessary, because you can't specify a has_many through: on a polymorphic association:
rb
has_many :recipients, through: :mailable
# => Cannot have a has_many :through association 'Mailing#recipients' which goes through the polymorphic association 'Mailing#mailable'. (ActiveRecord::HasManyThroughAssociationPolymorphicThroughError)
I tried to solve this with an Array field, but failed because it needs an ArrayResource to back it.
Is there any other way to achieve this?loud-jewelry-99127
04/07/2025, 10:16 AMmillions-exabyte-27721
04/07/2025, 10:21 AMrecipient resource, i.e. the User resource. but that doesn't work because it needs an Array resource:
rb
field :recipients, as: :array, use_resource: "User"millions-exabyte-27721
04/07/2025, 10:22 AMundefined method `fetch_records' for #<Avo::Resources::User:0x000000012edddc70>millions-exabyte-27721
04/07/2025, 10:23 AMArray ones 🤔loud-jewelry-99127
04/07/2025, 10:24 AMmillions-exabyte-27721
04/07/2025, 10:24 AMmillions-exabyte-27721
04/07/2025, 10:25 AMarray field is just a crutch anyway, it's just a has_many, just on another resource 🤔loud-jewelry-99127
04/07/2025, 10:27 AMmillions-exabyte-27721
04/07/2025, 10:29 AMloud-jewelry-99127
04/07/2025, 10:50 AMhas_many :recipients, -> (mailing) { mailing.recipients }millions-exabyte-27721
04/07/2025, 10:50 AMhas_many it actually works:
rb
# this is a dummy association to appease Avo
has_many :recipients, class_name: "User"
delegate :recipients, to: :mailable
rb
field :recipients, as: :has_many, use_resource: "User", name: "Recipients"loud-jewelry-99127
04/07/2025, 10:51 AMruby
class Mailing < ApplicationRecord
belongs_to :mailable, polymorphic: true
has_many :recipients, -> (mailing) { mailing.recipients }
end
I dont have the context setted up but probbaly this works ☝️millions-exabyte-27721
04/07/2025, 10:52 AMhas_many fields cannot be set to readonly, right?millions-exabyte-27721
04/07/2025, 10:52 AMcount query it issued. but nevermindloud-jewelry-99127
04/07/2025, 10:53 AMhas_many :recipients, -> (mailing) { mailing.mailable.recipients }loud-jewelry-99127
04/07/2025, 10:53 AMmailing.mailableloud-jewelry-99127
04/07/2025, 10:54 AMmillions-exabyte-27721
04/07/2025, 10:54 AMActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column users.mailing_id does not exist
don't worry thoughloud-jewelry-99127
04/07/2025, 10:55 AMmillions-exabyte-27721
04/07/2025, 10:55 AMdelegate more or less is just overriding the getter, which is good in my case because I don't even want that list to be editable, I just want to display it for referencemillions-exabyte-27721
04/07/2025, 10:55 AMmillions-exabyte-27721
04/07/2025, 10:56 AMmillions-exabyte-27721
04/07/2025, 10:57 AMloud-jewelry-99127
04/07/2025, 10:58 AMloud-jewelry-99127
04/07/2025, 10:58 AMmillions-exabyte-27721
04/07/2025, 10:59 AMloud-jewelry-99127
04/07/2025, 10:59 AMloud-jewelry-99127
04/07/2025, 11:00 AMdef view_recipients? = true then same for each action that you want to enable def edit_recipients? = true etc...loud-jewelry-99127
04/07/2025, 11:01 AMmillions-exabyte-27721
04/07/2025, 12:14 PMmillions-exabyte-27721
04/07/2025, 12:23 PMloud-jewelry-99127
04/07/2025, 12:24 PMmillions-exabyte-27721
04/07/2025, 12:25 PMmillions-exabyte-27721
04/07/2025, 12:25 PMloud-jewelry-99127
04/07/2025, 12:26 PMmillions-exabyte-27721
04/07/2025, 12:27 PMloud-jewelry-99127
04/07/2025, 12:28 PMmillions-exabyte-27721
04/07/2025, 12:56 PMmillions-exabyte-27721
04/07/2025, 12:57 PMloud-jewelry-99127
04/07/2025, 1:00 PM