Enum.reduce与Ecto.Multi

时间:2016-08-25 07:07:49

标签: phoenix-framework ecto

我想缩小表单的地图 %{"overall" => "2016-08-27", "2" => "2016-06-27", "8" => "2015-08-27"}具有这两个功能

def set_agreed_date(id, agreed_date) do
    Repo.get(Job, id)
      |> change(agreed_date: agreed_date)
      # |> Repo.update      # removed per Dogbert's comment
end

def update(conn, %{"id" => job_id, "agreed_dates" => agreed_dates}, user) do
    update = Enum.reduce(agreed_dates, Multi.new, fn({k, v}, acc) ->
                    {:ok, d} = Ecto.Date.cast v
                    case k do
                        "overall" ->
                            Multi.update(acc, "overall", set_agreed_date(job_id, d))
                        _ ->
                            Multi.update(acc, k, ShipToController.set_agreed_date(k, d))
                    end
                end)
    case Repo.transaction(update) do
        {:ok, ?? not sure what I will get here ??} -> ...

但我正在

[error] #PID<0.972.0> running Api.Endpoint terminated
Server: localhost:4000 (http)
Request: PUT /api/abc/14
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Ecto.Multi.add_operation/3
    (ecto) lib/ecto/multi.ex:331: Ecto.Multi.add_operation(%Ecto.Multi{names: #MapSet<[]>, operations: []}, "overall", {:changeset, #Ecto.Changeset<action: :update, changes: %{agreed_date: #Ecto.Date<2016-08-27>}, errors: [], data: #Api.Job<>, valid?: true>, []})

        (stdlib) lists.erl:1263: :lists.foldl/3
        (api) web/controllers/controller.ex:71: Api.Controller.update/3
        (api) web/controllers/controller.ex:1: Api.Controller.action/2
        (api) web/controllers/controller.ex:1: Api.Controller.phoenix_controller_pipeline/2
        (api) lib/api/endpoint.ex:1: Api.Endpoint.instrument/4
        (api) lib/phoenix/router.ex:261: Api.Router.dispatch/2
        (api) web/router.ex:1: Api.Router.do_call/2
        (api) lib/api/endpoint.ex:1: Api.Endpoint.phoenix_pipeline/1
        (api) lib/plug/debugger.ex:93: Api.Endpoint."call (overridable 3)"/2
        (api) lib/api/endpoint.ex:1: Api.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

0 个答案:

没有答案