agonzalezro
Hi ![]()
I had this piece of (working) code:
items =
products
|> Enum.map(fn product ->
params = %{product_id: product.id}
Snapshot.new_item_changeset(
%Snapshot.Item{
product: product
},
params
)
end)
That later created a form with:
form =
Ecto.Changeset.put_embed(
Ecto.Changeset.change(%Snapshot{}),
:items,
items
)
|> to_form
That I was rendering as:
<.form for={@form} phx-change="validate" phx-submit="save">
<.inputs_for :let={snapshot} field={@form[:items]}>
<div><%= snapshot.data.product.description %></div>
However, since I didn’t want to repeat that Ecto.Changeset.put_embed( I wanted to move that logic to the schema as:
def changeset(snapshot, params \\ %{}) do
snapshot
|> cast(params, [])
|> cast_embed(:items, with: &new_item_changeset/2)
end
def new_item_changeset(item, params \\ %{}) do
item
|> cast(params, [:quantity, :product_id, :rate_id])
|> validate_number(:quantity, greater_than_or_equal_to: 0)
end
But if I move the original code to:
items =
products
|> Enum.map(fn product ->
%{product_id: product.id}
end)
And then just:
form =
Snapshot.changeset(%Snapshot{}, %{items: items})
|> to_form
That works, however the template doesn’t have a product loaded ending up in the following error:
key :description not found in: #Ecto.Association.NotLoaded<association :product is not loaded>
It would be easy to fix this with a Repo.preload but since it’s an embedded schema I am not sure where to do that.
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
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
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
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
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
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
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
i-n-g-m-a-r
So first you assigned a product and later you only assigned an ID and then you lost the product?
agonzalezro
Thanks for your reply @i-n-g-m-a-r,
when you say that I “assigned” something you mean that I have a list of
productsand I just map it to get theproduct.id?I was doing it like that because in the past I was using the
productwhen creating theSnapshot.Item{}(you can see that in the first snippet), however, now that I want to useSnapshot.changesetto create the embedded version I don’t know how to set it or preload it.i-n-g-m-a-r
I mean assigning a value to a variable, like so:
product: product.Before you were loading product data into a changeset.
Now you seem to disregard everything but the product ID.
So it makes sense you would end up with an ID only.
Changesets don’t do any (pre)loading.
Their function is to guarantee valid data.
agonzalezro
Thanks again for your response @i-n-g-m-a-r.
What do you think would be the best approach to solve this issue then? Should I send a products map to the template and use the changeset product_id to read the product info from the map?
It seems there are ways to solve this, I am just not sure what would be the “most correct” (if that even exists
) in Phoenix’s world. I thought that even when the changeset wouldn’t take the
productinto consideration and just use theproduct_idrelation that was the proper way to do it.Any insight would be welcome!
i-n-g-m-a-r
Actually when I started using ecto it was not easy for me to fully grasp the power of changesets.
The concept is very powerful, you just need some experience to “get it”.
I’m not the right guy to ask about “correctness”.
Personally I use a macro to build schema’s automatically inserting changeset functions and all kinds of pre and post data transformation hooks.
I don’t think anyone considers that to be “correct”.
I think this part is a good starting point:
Snapshot.changeset(%Snapshot{}, %{items: items})I would focus first on fetching items.
Making sure every item is a certain struct.
Validated using changesets.
Then when you want to load a list of structs into a list of another type of structs, you can first (recursively) transform the list into a list of “params” (a nested map) and then load the params into a changeset for every item.
This should “just” work, no need for put_embed.
Also when you have a list of structs, you could load them into a schema embedding a list of structs (embeds_many).
Then you would have a (guaranteed) “type” instead of (just) a list.
In order to embed schema’s that are stored as records in a database you can create schemaless “meta” models.
I do this all the time.
Repo.all |> to_meta |> to_formSubmit…
params |> to_meta |> validate |> to_schema |> Repo.update_allI would want my interface to look like this:
Snapshot.changeset(params)Or like this:
Snapshot.changeset(stored_struct, params)agonzalezro
Thanks for the proposals.
I finally fixed my initial issue by also assigning to the socket a map of product id to product:
Then on the form I am showing it as follows: