How to do mixed / polymorphic relationships in Ash?

Hi,

I have a Project resource. It has many Note and many Product resources as relationships.

I would like to present a list of these two types of resources mixed in the UI – they will be in folders in a tree view. I can load all the resources in the live_view and make a mixed list there, but I would rather use relationships and have it be available through the e.g. the API and get pagination and stuff as well.

I saw the documentation for polymorphic resources, but I did not figure out how one relationship can source from different tables, and how e.g. when loading the resulting struct is a mixed array, of in my case Note and Product resources.

Is that possible, or will I have to define a custom relationship?

I’ve been meaning to write a guide on this. Let me put something together and post it here. Ash does not have “polymorphic relationships” per-se, but we actually have great tools for this kind of thing :slight_smile:

4 Likes

How’s this?

https://hexdocs.pm/ash/polymorphic-relationships.html

3 Likes

Very nice, think this was exactly what I was missing.

2 Likes