I have an item table which I want to have multiple embedded schema like:
schema "items" do
field :name, :string
field :type, :string
#embed_one with :either, I found this somewhere online but can't get the doc anywhere.
embeds_one :details, either: [ Product, Service], on_replace: :update
@doc false
def changeset(item, attrs) do
item
|> cast(attrs, [:name, :type, :description, :publish, :user_id])
|> details_by_type(attrs[:type], :details)
end
#psuedo code of cast_embed
defp details_by_type(item, type, details) do
case type do
:product -> item |> #cast_embed of :details to Product
:service -> item |> #cast_embed of :details to Service
end
end
Basically, I want to have a details field(:map) that keep different embedded schema based on the type of the item. But I can't get it to work. I keep getting error like this:
(ArgumentError) you attempted to apply a function on [either: [OkBackend.Items.Task, OkBackend.Items.Product, OkBackend.Items.Service], on_replace: :update]. Modules (the first argument of apply) must always be an atom
What is the right way of doing this?