elixir - 使用空卷预加载时协议 Enumerable 未实现错误
elixir - protocol Enumerable not implemented error when preload with empty roll
我有这个代码:
q = from p in Case, where: p.user_id == ^user_id:
Repo.all(q)
|> Repo.preload(:helper)
具有视图渲染功能:
alias ChatWeb.HelperView
def render("api_case.json", %{case: case, message: message, token: token}) do
%{
status: "1",
token: token.token,
items: case.items,
helpers: render_many(case.helper, HelperView, "helper.json"),
}
end
如果我有helper
个数据,一切就OK了。
但如果我在 helper
中没有任何东西,我将得到:
protocol Enumerable not implemented for
#Ecto.Association.NotLoaded of type Ecto.Association.NotLoaded (a struct)
我该如何解决这个问题?
不太清楚“如果我在 helper 中没有任何东西”是什么意思,但以下内容可能会:
def render("api_case.json", %{case: case, message: message, token: token}) do
helpers =
case case.helper do
%Ecto.Association.NotLoaded{} -> []
many -> render_many(many, HelperView, "helper.json")
end
%{status: "1", token: token.token, items: case.items, helpers: helpers}
end
我有这个代码:
q = from p in Case, where: p.user_id == ^user_id:
Repo.all(q)
|> Repo.preload(:helper)
具有视图渲染功能:
alias ChatWeb.HelperView
def render("api_case.json", %{case: case, message: message, token: token}) do
%{
status: "1",
token: token.token,
items: case.items,
helpers: render_many(case.helper, HelperView, "helper.json"),
}
end
如果我有helper
个数据,一切就OK了。
但如果我在 helper
中没有任何东西,我将得到:
protocol Enumerable not implemented for #Ecto.Association.NotLoaded of type Ecto.Association.NotLoaded (a struct)
我该如何解决这个问题?
不太清楚“如果我在 helper 中没有任何东西”是什么意思,但以下内容可能会:
def render("api_case.json", %{case: case, message: message, token: token}) do
helpers =
case case.helper do
%Ecto.Association.NotLoaded{} -> []
many -> render_many(many, HelperView, "helper.json")
end
%{status: "1", token: token.token, items: case.items, helpers: helpers}
end