Skip to content

Instantly share code, notes, and snippets.

@shankardevy
Last active July 8, 2017 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankardevy/bfbba0cab59eca0c01ca0f103224246b to your computer and use it in GitHub Desktop.
Save shankardevy/bfbba0cab59eca0c01ca0f103224246b to your computer and use it in GitHub Desktop.
defmodule Mango.Sales.Order do
use Ecto.Schema
import Ecto.Changeset
alias Mango.Sales.{Order, LineItem}
schema "orders" do
field :status, :string
field :total, :decimal
# Add the following embedded association and remove previous field definition for `line_items`
embeds_many :line_items, LineItem, on_replace: :delete
timestamps()
end
@doc false
def changeset(%Order{} = order, attrs) do
order
|> cast(attrs, [:status, :total, :line_items])
|> validate_required([:status, :total, :line_items])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment