Does Ash supports polymorphic embedded resources?

Yes :slight_smile:

defmodule YourEmbed do
  ...
end


defmodule YourOtherEmbed do
  ...
end
attribute :one_or_the_other, :union do
  constraints types: [
    one: [type: YourEmbed],
    other: [type: YourOtherEmbed]
  ]
end

You can also extract that out into a NewType

defmodule OneOrTheOther do
  use Ash.Type.NewType, subtype_of: :union, constraints: [
    types: [
      one: [type: YourEmbed],
      other: [type: YourOtherEmbed]
    ]
  ]
end

Then you can use the type like so:

attribute :one_or_the_other, OneOrTheOther

Check the docs for unions, as you may want some of the additional tools around it, like tag and tag_value to configure a type field and the value it should equal for it to be considered one type or the other.

The union type can also be used with primitives.

7 Likes