Assuming that I have Upload
and Entry
schemas, where Upload
is associated with Entry
with has_many
, is it possible to obtain validation results from Entry
when calling Upload.changeset
?
To illustrate I have defined the following:
defmodule MyApp.Upload do
# all the imports
schema "uploads" do
has_many :entries, Entry
end
def changeset(upload, attrs \\ %{}) do
upload
|> cast(attrs, [])
|> cast_assoc(:entries, with: &Entry.changeset/2)
end
end
defmodule MyApp.Entry do
# imports...
schema "entries" do
field :name, :string
field :animal, :string
belongs_to :upload, MyApp.Upload
end
def changeset(entry, attrs \\ %{}) do
entry
|> cast(attrs, [:name, :animal])
|> validate_required([:name, :animal])
end
end
However, calling
changeset = Upload.changeset(%Upload{entries: %Entry{name: name, animal: animal}})
never seem to actually call Entry.changeset
and I can not retrieve validation errors.
What am I doing wrong?