How do chain the context when calling API methods inside an Ash after_create?

I am trying to write an after_action in Ash, like this:


    update :create_note do
      accept []

      change fn changeset, context ->
        Ash.Changeset.after_action(changeset, fn changeset, user ->
          note_attrs = %{
            name: "note 1",
            description: "Default note",
            user: user
          }

          practice = Project.Api.Note
          |> Ash.Changeset.for_create(:create, note_attrs)
          |> Project.Api.create!(authorize?: context.authorize?)

          {:ok, user}
        end)
      end
    end

I am wondering specifically about this line:

Project.Api.create!(authorize?: context.authorize?)

Is there a better way to chain the ‘context’ arguments through? Like, if there was an actor, could I send that into “Project.Api.create” without having to explicitly do:

    Project.Api.create!(authorize?: context.authorize?, actor: context.actor)

Yes, you can use Ash.context_to_options/1

          practice = Project.Api.Note
          |> Ash.Changeset.for_create(:create, note_attrs)
          |> Project.Api.create!(Ash.context_to_opts(context, optional_extra: :opts))

thank you!

1 Like