Nested embeded schemas

You don’t need an extra TimeOfOccurance module if you decided to declare it inline. @dimitarvp suggested a single line change which will at the end look like

defmodule AngotiaCatalogsApi.NPCS.Settings do
  use Ecto.Schema
  import Ecto.Changeset

  embedded_schema do
    embeds_one :time_of_occurance, TimeOfOccurance do
      field :min, :integer
      field :max, :integer
    end
  end

  def settings_changeset(settings, attrs) do
    settings
    |> cast(attrs, [:time_of_occurance])
    |> cast_embed(:time_of_occurance, with: &time_of_occurance_changeset/2)
  end

  defp time_of_occurance_changeset(occurance, attrs) do
    occurance
    |> cast(attrs, [:min, :max])
  end
end

which compiles without any problems

If you still have an issue better to share with us a changed version of your code.

1 Like