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
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 6 of 6 Posts
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: