Hey there!
Goal
I like to have a function in each of my Models/Schemas/Domain-Objects/… that returns a pre-filled dataset to be used as an example.
Example
For a Person Ash resource:
defmodule MyApp.Person do
use Ash.Resource, data_layer: :embedded
actions do
defaults create: :*, update: :*
end
attributes do
attribute :salutation, :atom, constraints: [one_of: [:mr, :mrs, :ms, :other, :unknown]]
attribute :given_name, :string
attribute :name, :string
end
end
Problem
I want to add a function like the following within the resource:
def example_person do
%MyApp.Person{
salutation: :mr,
given_name: "Erik",
name: "Example"
}
end
This leads to compilation errors with a cyclic dependency, because the function requires MyApp.Person, but MyApp.Person contains the function.
Other attempts
I attempted to just return Maps, which works fine of course.
Then, at some point, multiple of those Maps are combined to a bigger resource. In this case, multiple pieces of information (like Person, Address, Files) are combined to an Application (for a job). Person, Address, Files are embedded resources.
When I have the pre-filled Map with all the example pieces
params = %{
person: MyApp.Person.example_data(), # map
address: MyApp.Address.example_data(), # map
files: [MyApp.File.example_data(), MyApp.File.example_data()] # list of maps
}
and do
Ash.Changeset.for_create(MyApp.Application, :create, params)
I get an invalid changeset and for each field saying the following:
%Ash.Error.Invalid.NoSuchInput{
calculation: nil,
resource: MyApp.Application,
action: :create,
input: :person,
inputs: MapSet.new([]),
did_you_mean: [],
splode: nil,
bread_crumbs: [],
vars: [],
path: [],
stacktrace: #Splode.Stacktrace<>,
class: :invalid
}
How to?
There is probably a very fundamental thing about Ash I do not understand yet.
- How would you go about creating example data for Ash Resources?
- Why do I get
Ash.Error.Invalid.NoSuchInput?






















