Created
April 13, 2021 20:32
-
-
Save joeljuca/c803d7f891355b9b199f93090a24ecf0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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