【问题标题】:cast/3 in Elixir Ecto changesetscast/3 在 Elixir Ecto 变更集中
【发布时间】:2017-02-01 14:19:54
【问题描述】:

我有以下型号。我是否必须在演员表中包含所有字段名称,或者是否有任何默认方式将参数中的所有字段映射到模型。

defmodule Chemical.Info do
    use Chemical.Web, :model

    schema "infos" do
        field :title, :string
        field :shortdesc, :string
        field :longdesc, :string
        field :images, :string
        field :regions, :string
        field :startdate, :date
        field :enddate, :date
        field :status, :string
        field :createdby, :string
        field :approvedby, :string

        timestamps()
    end

    def changeset(model, params \\ :empty) do
        model
        |> cast(params, ["title", "shortdesc", "longdesc"])
    end
end

是否必须指定强制转换方法中的所有字段名称才能复制到模型中?

【问题讨论】:

    标签: elixir phoenix-framework ecto


    【解决方案1】:

    我认为这可能有效,但我从未这样做过 ->

    Enum.map(@ecto_fields, &(elem(&1, 0)))
    

    【讨论】:

    • 我用这个 |> cast(params, Enum.map(@ecto_fields, &(elem( &1, 0)))) 并且它起作用了。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多