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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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

Other Trending Topics Top

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
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews