Ecto.Queryable 未针对 [....] 实施

Ecto.Queryable not implemented for [....]

我和Fix protocol Ecto.Queryable not implemented error有同样的错误,但我认为是不同的情况[​​=15=]

我想(按照凤凰之书)将可以删除的 Estimate 限制为用户拥有的那些,除非他们具有管理员权限。

  def delete(conn, %{"id" => id}, user) do
    user_estimates =
        case user.customer_id == 1 do
            true ->
                IO.inspect("Admin")
                Repo.all(Estimate)
            false ->
                IO.inspect("Non-Admin")
                assoc(user, :estimates)
        end
    estimate = Repo.get!(user_estimates, id)
    Repo.delete!(estimate)

但是当我以管理员身份使用此功能时,我得到

** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for [list of all Estimates]

我误会了什么?

问题在

Repo.all(Estimate)

Repo.all 实际上执行传递的查询,return 将结果作为列表。如果您想要包含所有估计值的 Ecto.Queryable,只需 return Estimate.

这应该有效:

user_estimates =
  case user.customer_id == 1 do
    true ->
      IO.inspect("Admin")
      Estimate
    false ->
      IO.inspect("Non-Admin")
      assoc(user, :estimates)
  end