nheingit

nheingit

Hello, I set up an ash resource that has some embedded resources.

(As a side note, I’d love some array helpers for the ash_forms, as I didn’t really want to create embedded resources in the first place, it could have just been an {:array, :string} but I didn’t see a clean way to map that to the helpers that exist currently! If you agree I’d love to help out with a PR on that)

But I’m trying to render some default values in the forms, and I don’t quite understand how to do that. My form looks like this currently.

      AshPhoenix.Form.for_create(RunConfigData, :create,
        api: Smol.Pipelines,
        actor: socket.assigns.current_user,
        forms: [
          models: [
            type: :list,
            resource: Model,
            data: [
              %{
                "model" => "gpt-4o"
              },
              %{
                "model" => "claude-3-opus-20240229"
              }
            ],
            create_action: :create,
            update_action: :update
          ],
          messages: [
            type: :list,
            resource: Message,
            data: [
              %{
                "message" => "msg1"
              },
              %{
                "message" => "msg2"
              }
            ],
            create_action: :create,
            update_action: :update
          ]
        ]
      )
      |> to_form()

And when I render the forms with the inputs_for:

            <.inputs_for :let={model_form} field={@run_config_form[:models]}>
              <div class="flex space-x-2 mt-1">
                <.input
                  type="text"
                  field={model_form[:model]}
                  phx-debounce="blur"
                  class="block w-full shadow-sm sm:text-sm border-gray-300 rounded-md"
                />
              </div>
            </.inputs_for>

It works in the sense that it will render two inputs, so it’s being picked up and that’s great, but I’d like those default value I set in the liveview to populate the value of the input when the page is first loaded.

So the question is, in this situation how do I access the value of the object in the inputs_for loop?

I do it with the other values in my form that aren’t embedded resources in the actual action of the resource like this:

defmodule Smol.Pipelines.RunConfigData do
  alias Smol.Pipelines.{Model, Message}

  use Ash.Resource,
    # Use in-memory storage since it doesn't need to persist
    data_layer: Ash.DataLayer.Ets

  attributes do
    uuid_primary_key :id
    attribute :repository_location_name, :string, allow_nil?: false
    attribute :job_name, :string, allow_nil?: false

    attribute :data_fetch_end_time, :utc_datetime, allow_nil?: false
    attribute :data_fetch_duration_hours, :integer, allow_nil?: false
    attribute :list_id, :integer, allow_nil?: false

    attribute :models, {:array, Model}, allow_nil?: false
    attribute :messages, {:array, Message}, allow_nil?: false
    attribute :system_prompt, :string, allow_nil?: false
    attribute :overrides, :map, allow_nil?: true
  end

  actions do
    defaults [:update]

    create :create do
      change fn changeset, _ ->
        changeset
        |> maybe_set_default(:repository_location_name, "data_ingestion")
        |> maybe_set_default(:job_name, "data_summary_job")
        |> maybe_set_default(:list_id, 1_585_430_245_762_441_216)
        |> maybe_set_default(:system_prompt, "Put your system prompt here")
        |> maybe_set_default(:data_fetch_duration_hours, 24)
        |> maybe_set_default(:overrides, nil)
      end
    end
  end

  defp maybe_set_default(changeset, field, default_value) do
    if Ash.Changeset.get_attribute(changeset, field) == nil do
      Ash.Changeset.change_attribute(changeset, field, default_value)
    else
      changeset
    end
  end
end

But I don’t quite understand how I would do that in the embedded resources.

Sorry if all of this isn’t quite clean, I’m quite new to Ash and am still getting my footing on how to do things, so please let me know if I’m doing something weird!

Showing Posts 1 to 1

zachdaniel

zachdaniel

Creator of Ash
  1. Would definitely love some array helpers for ash_phoenix. we have a built in embed called WrappedValue and if you call inputs_for on a value that is a list we can render a form per item in WrappedValue forms. May need to do some work internally to clean up the paramification of them automatically though, would need to double check

  2. Setting the default values for embedded forms in the action would be pretty tough, NGL. You’d be best off doing it in the the LiveView probably, like setting some params in a preflight call to validate or something along those lines. I’d have to think harder on it, but my suggestion is that this kind of defaulting is more about the UI layer than the model itself, and so it should probably go in the LiveView.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews