BrightEyesDavid

BrightEyesDavid

I have two schemas which are similar and which each contain the same embedded schema. One represents user-submitted data (submission) and one represents admin-created data which may or may not be based on a user submission (entry).

For entry’s new (from a submission) web admin action, so that the form inputs are prefilled with the submission’s data, how can I convert one Ecto submission result (a struct) and its embedded schemas (also structs) into a nested map in order to use it as params for a new changeset?

This would be from something like:

%MyApp.Submission{
  __meta__: #Ecto.Schema.Metadata<:loaded, "submissions">,
  entries: nil,
  data: %MyApp.Data{
    foo: true,
    TemperatureData: [
      %MyApp.Data.TemperatureOffset{
        Offset: -3,
        Temperature: -24,
        id: "1ffd0e3f-d5d9-4d1e-9a99-ae190dddde08"
      },
      %MyApp.Data.TemperatureOffset{
        Offset: 3,
        Temperature: 10,
        id: "7cd90b5b-19e1-4d4a-a68b-0d783ba76412"
      }
    ],
    id: "0b47e792-fc45-4c48-b476-877ede66aeda"
  },
  id: "0540750a-3ef3-4cf0-aa37-23cda4cb5d39",
  name: "Test"
}

to:

%{
  data: %{
    HVT: true,
    TemperatureData: [
      %{
        Offset: -3,
        Temperature: -24
      },
      %{
        Offset: 3,
        Temperature: 10
      }
    ]
  },
  id: "0540750a-3ef3-4cf0-aa37-23cda4cb5d39",
  name: "Test"
}

(In the example result I’ve removed the embedded schema’s ids, though I’m not sure this is necessary.)

Thanks.


Edit: apologies - I’ve realised (I think) this can be achieved simply by using changeset functions. If I understand correctly, I just need to remove the submission’s id before using it to make a new changeset.

Showing Posts 1 to 1

BrightEyesDavid

BrightEyesDavid OP

I currently have the following which seems to be doing the trick.

Controller:

def new(conn, %{"from_submission" => from_submission_id}) do
  submission = WorkData.get_submission!(from_submission_id)
  changeset = WorkData.create_entry_changeset_from_submission(submission)
  # ...
end

Context:

def create_entry_changeset_from_submission(%Submission{} = submission) do
  Entry.changeset_from_submission(submission)
end

Entry Schema:

def changeset_from_submission(submission) do
  %Entry{}
  |> changeset(%{})
  |> put_change(:data, submission.data)
  |> put_change(:type, submission.type)
  |> put_change(:name, submission.name)
end

Does this seem like a sane/reasonable way of creating a changeset in order to prefill entry HTML form fields with values from a submission?

It avoids what I get when I try to throw the whole single submission result into a changeset, even after using Map.from_struct/1:

[debug] ** (Ecto.CastError) expected params to be a :map, got: %MyApp.MyModule.WorkData.TemperatureOffsetProfile{foo: true, TemperatureData: [%MyApp.MyModule.WorkData.TemperatureOffset{Offset: -3, Temperature: -24, id: "1ffd0e3f-d5d9-4d1e-9a99-ae190dddde08"}, %MyApp.MyModule.WorkData.TemperatureOffset{Offset: 3, Temperature: 10, id: "7cd90b5b-19e1-4d4a-a68b-0d783ba76412"}], id: "0b47e792-fc45-4c48-b476-877ede66aeda"}

— 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
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
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
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
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews