agonzalezro
Preload associations with cast_embed
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.
Marked As Solved
agonzalezro
Thanks for the proposals.
I finally fixed my initial issue by also assigning to the socket a map of product id to product:
%{"1" => %xxx.Products.Product{
Then on the form I am showing it as follows:
<div><%= @products[snapshot.params["product_id"] |> to_string].description %></div>
Popular in Questions
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
I would like to know what is the best IDE for elixir development?
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New








