Tôi đang cố viết một changeset đơn lẻ sẽ cập nhật một mô hình và chèn một liên kết. Tôi không thể tìm thấy ví dụ về cách sử dụng put_assoc/4
Ecto: Cách cập nhật và chèn liên kết vào một bản cập nhật bằng cách sử dụng put_assoc
order = order
|> Proj.Order.changeset(%{state: "error", error_count: order.error_count + 1})
|> Ecto.Changeset.put_assoc(
:order_errors,
[Proj.OrderError.changeset(%Proj.OrderError{}, %{reason: "not_found"})])
|> Proj.Repo.update!
này in ra các lỗi sau:
** (Ecto.InvalidChangesetError) could not perform update because changeset is invalid.
* Changeset changes
%{order_errors: [%Ecto.Changeset{action: :insert, changes: %{id: nil, inserted_at: nil, order_id: nil, reason: "not_found", updated_at: nil}, constraints: [], errors: [order_id: "can't be blank"], filters: %{}, model: %Proj.OrderError{__meta__: #Ecto.Schema.Metadata<:built>, id: nil, inserted_at: nil, order: #Ecto.Association.NotLoaded<association :order is not loaded>, order_id: nil, reason: nil, updated_at: nil}, optional: [], opts: [], params: %{"reason" => "not_found"}, prepare: [], repo: nil, required: [:order_id, :reason], types: %{id: :id, inserted_at: Ecto.DateTime, order_id: :id, reason: :string, updated_at: Ecto.DateTime}, valid?: false, validations: []}], state: "error"}
* Changeset params
%{"error_count" => 1, "state" => "error"}
* Changeset errors
[]
Bất kỳ ví dụ tôi có thể nhìn vào cho put_assoc/4
? Làm thế nào tôi có thể tìm thấy lý do tại sao changeset không hợp lệ?
Mục tiêu của việc làm theo cách này là tôi hy vọng số order
mới sẽ có order_errors
được tải trước.