How to Modify Changeset at Save Time in Ash

Hey everyone! I’m working on an issue and could really use some guidance.

I start by creating a new changeset like this:

AshPhoenix.Form.for_create(Dummy.Service.Ticket, :create,
  as: "ticket",
  actor: socket.assigns.current_user
)

After the user hits the “Save” button, the form data is submitted using AshPhoenix.Form.submit. At this point, I’d like to modify the changeset to include an additional attribute:

change &Dummy.Service.Ticket.Changes.add_ticket_number/2

Is there a way to add this attribute to the changeset at the time of saving, rather than at the time of creation?

defmodule Dummy.Service.Ticket.Changes do
  def add_ticket_number(changeset, _context) do
    number =
      Ash.max!(Dummy.Service.Ticket, :number)
      |> increment_ticket_number()

    Ash.Changeset.change_attribute(changeset, :number, number)
  end

  def increment_ticket_number(old) do
    old + 1
  end
end

The way you should do this is with hooks :slight_smile:

defmodule AddTicketNumber do
  use Ash.Resource.Change

  def change(changeset, _, _) do
    Ash.Changeset.before_action(changeset, fn changeset -> 
      add_ticket_number(changeset)
    end)
  end
end

change AddTicketNumber