gpt4 book ai didi

elixir ecto - 具有多个模式的 embed_one

转载 作者:行者123 更新时间:2023-12-05 05:55:13 24 4
gpt4 key购买 nike

我有一个 item 表,我希望它有多个 embedded schema,例如:

  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

基本上,我想要一个details field(:map) 来根据项目的类型保留不同的嵌入式模式。但我无法让它工作。我不断收到这样的错误:

(ArgumentError) you attempted to apply a function on [either:[OkBackend.Items.Task, OkBackend.Items.Product,OkBackend.Items.Service], on_replace: :update]. Modules (the firstargument of apply) must always be an atom

这样做的正确方法是什么?

最佳答案

看起来这个包就是您要找的,这是文档中的示例:

defmodule MyApp.Reminder do
use Ecto.Schema
import Ecto.Changeset
import PolymorphicEmbed, only: [cast_polymorphic_embed: 3]

schema "reminders" do
field :date, :utc_datetime
field :text, :string

field :channel, PolymorphicEmbed,
types: [
sms: MyApp.Channel.SMS,
email: [module: MyApp.Channel.Email, identify_by_fields: [:address, :confirmed]]
],
on_type_not_found: :raise,
on_replace: :update
end

def changeset(struct, values) do
struct
|> cast(values, [:date, :text])
|> cast_polymorphic_embed(:channel, required: true)
|> validate_required(:date)
end
end

https://hexdocs.pm/polymorphic_embed/readme.html

关于elixir ecto - 具有多个模式的 embed_one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69480876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com