Ash FrameworkAF
Ash Framework6mo ago
20 replies
Igor Barakaiev

Adding new changeset arguments in `before_action`

Hi, I'm having a bit of trouble understanding the order in which changes get executed within an update action and how to set arguments. Suppose I have something like:

update :foo do
  require_atomic? false
  
  accept []

  argument :arg1, :string, allow_nil?: false

  change before_action(fn changeset, _context ->
    record = changeset.data

    nested_resource = NestedResource.create!(%{parent_id: record.id})

    Ash.Changeset.set_argument(changeset, :nested_resource_id, nested_resource.id)
  end)

  change fn changeset, _context ->
    dbg(changeset.arguments)

    changeset
  end
end


Running :foo on a resource prints the following in the IEx:

arguments #=> %{
  nil: "0198908a-c4ca-7c4f-831e-b67ea589f4c8",
  arg1: "description"
}


where the value associated with nil is the nested_resource.id.

I have two questions:
1. At what point do changes that are not before_action, etc get run?
2. Is this a bug that an argument gets a key of nil instead of :nested_resource_id?
Was this page helpful?