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

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
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement