Ash FrameworkAF
Ash Framework3y ago
11 replies
Stefan Wintermeyer

Create a resource including a belongs_to id

I have this resource with the name location:

attributes do
  uuid_primary_key :id

  attribute :name
  attribute :code, :string
  attribute :slug, :string
end

relationships do
  belongs_to :level, Feriendaten.Geo.Level
end


Now I want to create a new entry for this resource with this code:

 level =
    Feriendaten.Geo.Level
    |> Ash.Query.filter(contains(slug, "land"))
    |> Feriendaten.Geo.read!()
    |> hd()

  Feriendaten.Geo.Location
  |> Ash.Changeset.for_create(:create, %{
    name: "Germany",
    code: "DE",
    slug: "germany",
    level_id: level.id
  })
  |> Feriendaten.Geo.create!()


The recourse Location gets created but the attribute level_id is empty.

How can I achieve to create a Location with the belongs_to association?
Was this page helpful?