Permalink
Browse files

Add missing documentation for updating embeds (#2100)

  • Loading branch information...
12s12m authored and josevalim committed Jun 20, 2017
1 parent a871fbd commit 9962c146c0569a91eda189e328a7d02382d9bcc7
Showing with 23 additions and 0 deletions.
  1. +23 −0 lib/ecto/schema.ex
View
@@ -1274,6 +1274,29 @@ defmodule Ecto.Schema do
items
# => [%Item{id: "20a97d94-f79b-4e63-a875-85deed7719b7", title: "Soap"}]
Updating of embeds must be done using a changeset for each changed embed.
# Order has an existing items
order = Repo.get!(Order, 42)
order.items
# => [%Item{id: "20a97d94-f79b-4e63-a875-85deed7719b7", title: "Soap"}]
# Generate a changeset
changeset = Ecto.Changeset.change(order)
# Put the updated item as a changeset
current_item = List.first(order.items)
item_changeset = Ecto.Changeset.change(current_item, title: "Mujju's Soap")
order_changeset = Ecto.Changeset.put_embed(changeset, :items, [item_changeset])
# Update the order and fetch items
items = Repo.update!(order_changeset).items
# Item has the updated title
items
# => [%Item{id: "20a97d94-f79b-4e63-a875-85deed7719b7", title: "Mujju's Soap"}]
## Inline embedded schema
The schema module can be defined inline in the parent schema in simple

0 comments on commit 9962c14

Please sign in to comment.