Hello guys I can’t figure out why my association is not preloaded in my view.
When I POST an image with the associated category I’m getting a Internal Server Error because I’ve added in my image view the category’s view :
defmodule ApiAppWeb.ImageView do
use ApiAppWeb, :view
alias ApiAppWeb.ImageView
def render("index.json", %{image: image}) do
%{data: render_many(image, ImageView, "image.json")}
end
def render("show.json", %{image: image}) do
%{data: render_one(image, ImageView, "image.json")}
end
def render("image.json", %{image: image}) do
%{
id: image.id,
name: image.name,
description: image.description,
image: image.image,
category: render_one(image.category, ApiAppWeb.CategoriesView, "categories.json" )
}
end
end
So I’m getting the not loaded association error : #Ecto.Association.NotLoaded<association :category is not loaded>
But on my list_image method I preloaded the category and I’ve been through the documentation and it references the same way as I did:
def list_image do
Image
|> Repo.all()
|> Repo.preload(:category)
end
When I’m trying to debug the problem I can see that when I’m creating a new entry I need to load the association and when I’m making a GET request on my image list, the category view did not load the image association but I do get all the results… this is a little bit weird
Thanks a lot for your help !