sezaru

sezaru

Does Ash supports polymorphic embedded resources?

The title says it all, is it possible to create more than one embedded resource and use them both as a single attribute in a third resource?

I was thinking about something similar to what this library does for ecto: Polymorphic embeds for Ecto — Polymorphic Embed v5.0.6

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

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.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement