Unable to load relation with Ash.load!()

Repo: helpdesk_with_tests/test/helpdesk/support/representative_test.exs at main · albinkc/helpdesk_with_tests · GitHub

I want to assert that the representative’s name is as expected. I have defined the relationships as given in the tutorial.

test "assign representative to a ticket" do
    representative =
      Representative
      |> Ash.Changeset.for_create(:create, %{name: "Joe Armstrong"})
      |> Ash.create!()

    ticket =
      Ticket
      |> Ash.Changeset.for_create(:open, %{subject: "I can't find my hand!"})
      |> Ash.create!()

    ticket
    |> Ash.Changeset.for_update(:assign, %{representative_id: representative.id})
    |> Ash.update!()
    |> Ash.load!([:representative])

    assert {:ok, _} = Ecto.UUID.cast(ticket.id)
    # IO.inspect(ticket.representative)
    assert ticket.representative.name == "Joe Armstrong"
  end

This returns the following failure message:

  1) test assign representative to a ticket (Helpdesk.Support.RepresentativeTest)
     test/helpdesk/support/representative_test.exs:19
     ** (KeyError) key :name not found in: #Ash.NotLoaded<:relationship, field: :representative>
     code: assert ticket.representative.name == "Joe Armstrong"
     stacktrace:
       test/helpdesk/support/representative_test.exs:37: (test)

I tried various other combinations of Ash.load() but couldn’t find an example for this exact scenario in the docs.
Also, is ticket.representative.name the correct way to get the representative’s name?

Thanks in advance :slight_smile:

Is this the real code? Because it seems you’re not reassigning ticket in the update.

Welp! That was too simple to overlook. Elixir noobie here (if you couldn’t already tell). Thanks for the fix.