2016-05-14 76 views
1

我有相同的錯誤Fix protocol Ecto.Queryable not implemented error但我認爲有不同的情況Ecto.Queryable不執行[...]

我想(以下鳳凰書)限制Estimate s表示可以刪除給用戶擁有的,除非他們擁有管理員權限。

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)協議Ecto.Queryable不是[所有預算清單]

實施我誤解了什麼?

回答

1

的問題是在

Repo.all(Estimate) 

Repo.all實際執行通過查詢並返回結果列表。如果您想要包含所有估算值的Ecto.Queryable,請返回Estimate

這應該工作:

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