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
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
handle_info already has a Product struct, why Repo.get it again?
Popular in Questions
Other popular topics
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
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










