zingo

zingo

Error when listing posts with relations after updating

I’ll try to describe it as simple as possible.

I made a very simple project to play around with while trying to learn Elixir and Phoenlix Liveview. I created Categories and Products with phx.gen.live. A Product has a belongs_to-relationship with Category, and Category a has_many-relationship with Products.

Adding products and assigning category to them works fine. When hitting the save button the index lists all the products:

In my index-template I have the following code to list it as product name (category name).

<%= product.name %> (<%=product.category.name %>)

Hoever when I click edit on an item and change the category and hit save, the process crashes with the following error:

** (KeyError) key :name not found in: #Ecto.Association.NotLoaded<association :category is not loaded>

After the crashing process restarts everything works fine again, showing the correct new category in the index-view. I also get a quick red flash saying “Something went wrong”.

In live/product_live/index.ex I have modified the mount-function:

def mount(_params, _session, socket) do
  {:ok, stream(socket, :products, Products.list_products_test())}
end

And list_produts_test() is defined as:

def list_products_test do
  Repo.all(Product) |> Repo.preload([:category])
end

I guess I get the error because I have changed the category for one product, and when hitting save somehow the preload() is not called until the process crashes and is reloaded. Should I put the preload() somewhere else, will that solve my problem?

Marked As Solved

al2o3cr

al2o3cr

I suspect the best place to change is the code between these two spots - the part that calls update_product and then sends the {:saved, product} message.

You’d want to add a Repo.preload there to ensure that the Product being added to the stream has the needed association loaded.

Also Liked

al2o3cr

al2o3cr

handle_info already has a Product struct, why Repo.get it again?

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement