Skip to content

Instantly share code, notes, and snippets.

@joeljuca
Created April 13, 2021 20:32
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 joeljuca/c803d7f891355b9b199f93090a24ecf0 to your computer and use it in GitHub Desktop.
Save joeljuca/c803d7f891355b9b199f93090a24ecf0 to your computer and use it in GitHub Desktop.
# lib./my_app/entries.ex
defmodule MyApp.Entries do
alias MyApp.Entry
# Aqui, eu espero erros serem retornados (como tuplas)
# ao invés de serem lançados (com throw, etc.)
def create_entry(attrs \\ %{}) do
%Entry{}
|> Entry.changeset(attrs)
|> Repo.insert()
end
end
# lib./my_app/entries/entry.ex
defmodule MyApp.Entry do
use Ecto.Schema
import Ecto.Changeset
alias MyApp.Address
schema "entries" do
embeds_one(:address, Address, on_replace: :update)
field(:notes, :string)
timestamps(type: :utc_datetime)
end
def changeset(entry, attrs) do
entry
|> cast(attrs, [:notes])
|> cast_embed(:address)
|> validate_required([:address])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment